如何在Python中过滤和写入多个文件?

2024-03-28 11:29:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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


Tags: 文件ofcsvthe项目inmyitem
2条回答

你为什么不用熊猫

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')

在每次筛选之前,您需要返回到文件的顶部

在代码的筛选行之前插入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.

相关问题 更多 >