获取多个csv的特定行并写入新的csv

时间:2018-03-07 16:03:16

标签: python csv scripting

进行了很好的搜索但却无法找到我正在寻找的东西。我有一些CFD模拟打印的csv文件。我的python脚本的目标是:

  • 获取每个csv和
  • 的最后一行
  • 将行添加到新文件中,文件名添加到每行的开头

目前我有

if file.endswith(".csv"):

    with open(file, 'r') as f:
        tsrNum = file.translate(None, '.csv')
        print(tsrNum + ', ' + ', '.join(list(csv.reader(f))[-1]))

将正确的值打印到终端中,但我必须手动将其粘贴到新文件中。

有人可以帮助完成最后一步吗?我对python的语法不太熟悉,当我完成这个CFD项目时,肯定在我的待办事项列表中,因为到目前为止,当我设法正确地实现它时它非常棒。我尝试使用循环和csv.dictWriter,但收效甚微。

修改

我无法使发布的解决方案正常运行。这是一个人帮我制作的代码

import csv
import os
import glob

# get a list of the input files, all CSVs in current folder
infiles = glob.glob("*.csv")
# open output file
ofile = open('outputFile.csv', "w")
# column names
fieldnames = ['parameter','time', 'cp', 'cd']
# access it as a dictionary for easy access
writer = csv.DictWriter(ofile, fieldnames=fieldnames)
# output file header
writer.writeheader()
# iterate through list of input files
for ifilename in infiles:
    # open input file
    ifile = open(ifilename, "rb+")
    # access it as a dictionary for easy access
    reader = csv.DictReader(ifile)
    # get the rows in reverse order
    rows = list(reader)
    rows.reverse()
    # get the last row
    row = rows[0]
    # output row to output csv
    writer.writerow({'parameter': ifilename.translate(None, '.csv'), 'time': row['time'], 'cp': row['cp'], 'cd': row['cd']})
    # close input file
    ifile.close()

# close output file
ofile.close()

1 个答案:

答案 0 :(得分:0)

将问题分成小块:

  • 循环目录
  • 获取最后一行
  • 写信给你的新csv。

我试图非常冗长,所以你应该尝试这样做:

   import os

    def get_last_line_of_this_file(filename):
        with open(filename) as f:
            for line in f:
                pass
            return line

    def get_all_csv_filenames(directory):
        for filename in os.listdir(directory):
            if filename.endswith('.csv'):
                yield filename

    def write_your_new_csv_file(new_filename):
        with open(new_filename, 'w') as writer:
            for filename in get_all_csv_filenames('now the path to your dir'):
                last_line = get_last_line_of_this_file(filename)
                writer.write(filename + ' ' + last_line)

    if __name__ == '__main__':
        write_your_new_csv_file('your_created_filename.csv')