簡體   English   中英

如何在 Python 中過濾和寫入多個文件?

[英]How to filter and write to multiple files in Python?

如果有人可以提供幫助,我是 python 的新手,並且在這個項目上停留了幾天,謝謝。

我正在嘗試寫入多個輸出文件,每個輸出文件都包含來自一個原始輸入文件的過濾器結果。 我在過濾器下方的行中放置了打印語句,以顯示“項目”正在傳遞給語句,但是每當我查看輸出文件時,所有包含的都是標題。 例如,csv 文件中第 5 列的唯一列表是紅色、藍色、綠色。 創建與每種顏色相關聯的輸出文件 - 但內容始終為空。

輸出應該是當 item = blue Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8 1,2,3,4,Blue,6,7,8 1,2,3,4,Blue,6 ,7,8 1,2,3,4,藍色,6,7,8

輸出應該是當 item is = red 1,2,3,4,Red,6,7,8 1,2,3,4,Red,6,7,8 1,2,3,4,Red,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 輸入文件

Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8 1,2,3,4,Red,6,7,8 1,2,3,4,Blue,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) ,如下所示。

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)

修復是在每個過濾器之前重新定位到文件的前面,以便您根據需要過濾整個文件。

你為什么不使用熊貓?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM