如何过滤和写入Python中的多个文件?
How to filter and write to multiple files in Python?
我是 python 的新手,在这个项目上坚持了好几天,如果有人能提供帮助,谢谢。
我正在尝试写入多个输出文件,每个输出文件都包含来自一个原始输入文件的过滤器结果。我已将打印语句放在过滤器下面的行中,以向我显示 'item' 正在传递给该语句,但每当我查看输出文件时,所有包含的都是 headers。因此,例如,csv 文件中第 5 列的唯一列表是红色、蓝色、绿色。创建与每种颜色相关联的输出文件 - 但内容始终为空。
当 item = blue 时输出应该是
姓名 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
你为什么不用pandas?
import pandas as pd
df_col = pd.read_csv('colours.csv')
colours = ['Red', 'Blue', 'Green']
for colour in colours:
df_col[df_col['Name5'] == colour].to_csv(colour + '_out.csv')
您需要 return 在每个过滤器之前到文件的顶部。
在您的代码中的过滤器行之前插入 csv_file.seek(0),如下所示。
csv_file.seek(0) # Reposition to front of file
filtered = filter(lambda r: r['Name5'] == item, csv_reader)
说明
以下代码片段将您置于文件底部
for line in csv_reader:
my_list.add(line['Name5'])
还有:
filtered = filter(lambda r: r['Name5'] == item, csv_reader)
for row in filtered:
csv_writer.writerow(row)
Fix is to reposition to the front of the file before each filter so you're filtering the entire file as desired.
我是 python 的新手,在这个项目上坚持了好几天,如果有人能提供帮助,谢谢。
我正在尝试写入多个输出文件,每个输出文件都包含来自一个原始输入文件的过滤器结果。我已将打印语句放在过滤器下面的行中,以向我显示 'item' 正在传递给该语句,但每当我查看输出文件时,所有包含的都是 headers。因此,例如,csv 文件中第 5 列的唯一列表是红色、蓝色、绿色。创建与每种颜色相关联的输出文件 - 但内容始终为空。
当 item = blue 时输出应该是 姓名 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
你为什么不用pandas?
import pandas as pd
df_col = pd.read_csv('colours.csv')
colours = ['Red', 'Blue', 'Green']
for colour in colours:
df_col[df_col['Name5'] == colour].to_csv(colour + '_out.csv')
您需要 return 在每个过滤器之前到文件的顶部。
在您的代码中的过滤器行之前插入 csv_file.seek(0),如下所示。
csv_file.seek(0) # Reposition to front of file
filtered = filter(lambda r: r['Name5'] == item, csv_reader)
说明
以下代码片段将您置于文件底部
for line in csv_reader:
my_list.add(line['Name5'])
还有:
filtered = filter(lambda r: r['Name5'] == item, csv_reader)
for row in filtered:
csv_writer.writerow(row)
Fix is to reposition to the front of the file before each filter so you're filtering the entire file as desired.