简体   繁体   English

在python 2.7中将特定行从csv复制到csv

[英]Copy specific rows from csv to csv in Python 2.7

So far I have been trying to copy specific rows including headers from original csv file to a new one. 到目前为止,我一直在尝试将特定的行(包括标头)从原始的csv文件复制到新的行。 However, once I run my code it was copying a total mess creating a huge document. 但是,一旦我运行代码,它就会复制整个混乱,从而创建一个巨大的文档。

This is one of the options I have tried so far, which seems to be the closest to the solution: 这是我到目前为止尝试过的选项之一,似乎与解决方案最接近:

import csv
with open('D:/test.csv', 'r') as f,open('D:/out.csv', 'w') as f_out:
     reader = csv.DictReader(f)
     writer = csv.writer(f_out)
     for row in reader:
         if row["ICLEVEL"] == "1":
            writer.writerow(row)

The thing is that I have to copy only those rows where value of "ICLEVEL"(Header name) is equal to "1". 事实是,我只需要复制“ ICLEVEL”(标题名称)的值等于“ 1”的那些行。

Note: test.csv is very huge file and I cannot hardcode all header names in the writer. 注意:test.csv是非常大的文件,我无法在编写器中对所有标头名称进行硬编码。

Any demostration of pythonic way of doing this is greatly appreciated. 非常感谢使用pythonic方式进行的演示。 Thanks. 谢谢。

writer.writerow expects a sequence (a tuple or list). writer.writerow需要一个序列(元组或列表)。 You can use DictWriter which expects a dict . 您可以使用需要dict DictWriter

import csv
with open('D:/test.csv', 'r') as f, open('D:/out.csv', 'w') as f_out:
    reader = csv.DictReader(f)
    writer = csv.DictWriter(f_out, fieldnames=reader.fieldnames)
    writer.writeheader()  # For writing header
    for row in reader:
        if row['ICLEVEL'] == '1':
            writer.writerow(row)

Your row is a dictionary. 您的row是一本字典。 CSV writer cannot write dictionaries. CSV编写器无法编写字典。 Select the values from the dictionary and write just them: 从字典中选择值,然后只写它们:

writer.writerow(reader.fieldnames)
for row in reader:
  if row["ICLEVEL"] == "1":
    values = [row[field] for field in reader.fieldnames]
    writer.writerow(values)

I would actually use Pandas, not a CSV reader: 我实际上会使用Pandas,而不是CSV阅读器:

import pandas as pd

df=pd.read_csv("D:/test.csv")
newdf = df[df["ICLEVEL"]==1]
newdf.to_csv("D:/out.csv",index=False)

The code is much more compact. 代码更加紧凑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM