编写一个脚本,该脚本读取单个csv文件,并根据前两个字段中的值创建新的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