Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在单个循环中读取csv并创建csv/追加行_Python - Fatal编程技术网

Python 在单个循环中读取csv并创建csv/追加行

Python 在单个循环中读取csv并创建csv/追加行,python,Python,编写一个脚本,读取单个csv文件,并根据前2个字段中的值创建新的csv。然后重复第二次,将行附加到正确的csv 有没有一种更优雅的方法来实现这一点并降低执行时间?我可以在一次读取迭代中完成这项工作吗 import csv base_filename = '_Template_.csv' in_path = 'C:\\Users\\x\\Desktop\\Test\\' out_path = 'C:\\Users\\x\\Desktop\\Test\\Completed\\' in_filena

编写一个脚本,读取单个csv文件,并根据前2个字段中的值创建新的csv。然后重复第二次,将行附加到正确的csv

有没有一种更优雅的方法来实现这一点并降低执行时间?我可以在一次读取迭代中完成这项工作吗

import csv

base_filename = '_Template_.csv'
in_path = 'C:\\Users\\x\\Desktop\\Test\\'
out_path = 'C:\\Users\\x\\Desktop\\Test\\Completed\\'
in_filename = 'Source.csv'
in_file = open(in_path + in_filename, 'rb')
rdr = csv.reader(in_file)
rdr.next() #skip header row

for row in rdr: #create files and write header rows
    with open(out_path + row[1] + '_Template' + row[0] + '.csv', 'wb') as out_file: #uses values from fields 1 and 2 to create new filename
        wtr = csv.writer(out_file)
        wtr.writerow([str(x) for x in range(1,10)])

in_file.seek(1)

for row in rdr: #appened row to correct csv
    out_filename = row[1] + '_Template_' + row[0] + '.csv'
    split_filename = out_filename.split('_')
    if row[1] == split_filename[0] and row[0] == split_filename[2][:-4]: #check if value 1 & 2 equal values split from filename
        with open(out_path + out_filename, 'ab') as out_file:
            wtr = csv.writer(out_file)
            wtr.writerow((row[1:10]))
编辑

下面是源数据和所需输出的示例

Source.csv  
product,id,email,firstname,lastname,date,revenue,average,count1,count2
car,1,first.last@something.com,first,last,1/1/2015,600,50,1,2
car,1,first.last@something.com,first,last,1/2/2015,500,50,1,2
boat,1,first.last@something.com,first,last,1/3/2015,400,50,1,2
car,2,first.last@something.com,first,last,1/4/2015,300,50,1,2
boat,3,first.last@something.com,first,last,1/5/2015,200,50,1,2

1_Template_car.csv 
id,email,firstname,lastname,date,revenue,average,count1,count2 
1,first.last@something.com,first,last,1/1/2015,600,50,1,2
1,first.last@something.com,first,last,1/2/2015,500,50,1,2

1_Template_boat.csv
id,email,firstname,lastname,date,revenue,average,count1,count2
1,first.last@something.com,first,last,1/3/2015,400,50,1,2

2_Template_car.csv 
id,email,firstname,lastname,date,revenue,average,count1,count2 
2,first.last@something.com,first,last,1/4/2015,300,50,1,2

3_Template_boat.csv  
id,email,firstname,lastname,date,revenue,average,count1,count2
3,first.last@something.com,first,last,1/5/2015,200,50,1,2

示例csv和输出将有所帮助。代码的当前状态使其难以查看。您可以使用更多的中间变量并添加一些注释吗?例如,
base\u文件名[-4:]
似乎始终是扩展名
'.csv'
,因此可以这样称呼它。进行了适当的编辑。非常感谢!你可以试试看。我将CSV读入一个
数据帧
(请参阅)。然后,您只需使用前两列
对_进行分组,最后写入结果组。它应该是几行代码并且非常有效。