1

So far I have been trying to copy specific rows including headers from original csv file to a new one. 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".

Note: test.csv is very huge file and I cannot hardcode all header names in the writer.

Any demostration of pythonic way of doing this is greatly appreciated. Thanks.

Coke
  • 965
  • 2
  • 9
  • 22

3 Answers3

4

writer.writerow expects a sequence (a tuple or list). You can use DictWriter which expects a dict.

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)
Hussain
  • 5,057
  • 6
  • 45
  • 71
  • Thanks for an effort, but it gave me a key error ['ICLEVEL']. – Coke Dec 08 '16 at 06:25
  • Yes, I just double checked it again :( – Coke Dec 08 '16 at 06:30
  • It should have worked. Could you put few sample rows of your CSV? – Hussain Dec 08 '16 at 06:33
  • I just replaced my old csv with new one. It worked, however, it copies all the data, not specific one that I need. – Coke Dec 08 '16 at 06:36
  • There must be some whitespace around `row['ICLEVEL']`. It will be helpful if you add some snippet of your CSV. – Hussain Dec 08 '16 at 06:45
  • Possibly, that's very interesting, I will try to make your solution work as well. Appreciate your assistance. Thank you! – Coke Dec 08 '16 at 06:47
  • 1
    Passing `skipinitialspace=True` to the reader might help in that case http://stackoverflow.com/questions/14885908/strip-white-spaces-from-file-csv – Hussain Dec 08 '16 at 06:49
1

Your row is a dictionary. CSV writer cannot write dictionaries. 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)
DYZ
  • 55,249
  • 10
  • 64
  • 93
0

I would actually use Pandas, not a CSV reader:

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.

DYZ
  • 55,249
  • 10
  • 64
  • 93