我是python新手,如果有人能帮忙的话,我会坚持这个项目好几天,谢谢
我试图写入多个输出文件,每个输出文件都包含来自一个原始输入文件的筛选结果。我将prints语句放在过滤器下面的行中,以显示“item”正在传递给语句,但每当我查看输出文件时,包含的都是标题。例如,csv文件第5列中的唯一列表为红色、蓝色和绿色。将创建与每种颜色关联的输出文件,但内容始终为空
当项目为蓝色时,输出应为 名字1,名字2,名字3,名字4,名字5,名字6,名字7,名字8 1,2,3,4,蓝色,6,7,8 1,2,3,4,蓝色,6,7,8 1,2,3,4,蓝色,6,7,8
当项目为红色时,输出应为 1,2,3,4,红色,6,7,8 1,2,3,4,红色,6,7,8 1,2,3,4,红色,6,7,8
当项目为绿色时,输出应为
1,2,3,4,绿色,6,7,8
下面的程序
import csv
# opens a the input file and reads in the data
with open('Test_colours_in.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
# prints list of unique values in column 5 of csv of input file
my_list = set()
for line in csv_reader:
my_list.add(line['Name5'])
print(my_list)
# takes these unique values and creates files associated with each unique value
for item in my_list:
with open(item + '_'+'Test.csv', 'w', newline='') as new_file:
fieldnames = ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6', 'Name7', 'Name8']
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)
csv_writer.writeheader()
# filters the original file for each item in the list of unique values and writes them to respective file
filtered = filter(lambda r: r['Name5'] == item, csv_reader)
for row in filtered:
csv_writer.writerow(row)
csv输入文件
名字1,名字2,名字3,名字4,名字5,名字6,名字7,名字8 1,2,3,4,红色,6,7,8 1,2,3,4,蓝色,6,7,8 1,2,3,4,蓝色,6,7,8 1,2,3,4,蓝色,6,7,8 1,2,3,4,红色,6,7,8 1,2,3,4,红色,6,7,8 1,2,3,4,绿色,6,7,8
你为什么不用熊猫
在每次筛选之前,您需要返回到文件的顶部
在代码的筛选行之前插入csv_file.seek(0),如下所示
解释
以下代码片段将您放在文件的底部
还有:
相关问题 更多 >
编程相关推荐