回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我是python新手,如果有人能帮忙的话,我会坚持这个项目好几天,谢谢</p>
<p>我试图写入多个输出文件,每个输出文件都包含来自一个原始输入文件的筛选结果。我将prints语句放在过滤器下面的行中,以显示“item”正在传递给语句,但每当我查看输出文件时,包含的都是标题。例如,csv文件第5列中的唯一列表为红色、蓝色和绿色。将创建与每种颜色关联的输出文件,但内容始终为空</p>
<p>当项目为蓝色时,输出应为
名字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</p>
<p>当项目为红色时,输出应为
1,2,3,4,红色,6,7,8
1,2,3,4,红色,6,7,8
1,2,3,4,红色,6,7,8</p>
<p>当项目为绿色时,输出应为</p>
<p>1,2,3,4,绿色,6,7,8</p>
<p>下面的程序</p>
<pre><code>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)
</code></pre>
csv输入文件
<p>名字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</p>