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.

3
  • Are you getting any error? What's happening with your solution? Commented Dec 8, 2016 at 6:14
  • When I run it says "sequence expected" and points at the last row. However, if I go with writer.writerow([row]) it makes a total mess. Commented Dec 8, 2016 at 6:15
  • can you give csv data example? Commented Dec 8, 2016 at 6:17

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for an effort, but it gave me a key error ['ICLEVEL'].
Yes, I just double checked it again :(
It should have worked. Could you put few sample rows of your CSV?
I just replaced my old csv with new one. It worked, however, it copies all the data, not specific one that I need.
Passing skipinitialspace=True to the reader might help in that case stackoverflow.com/questions/14885908/…
|
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)

4 Comments

You did a great job. Can you suggest how to copy headers as well?
The first line of my snippet copies the headers.
Works for me in Python 2.7. How does it not work for you?
Thank you for the solution!
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.

3 Comments

I am not allowed to use Pandas. Your previous answer was correct, I would appreciate if you could restore it. Thanks.
Ok, I see. I believe @Hussain's answer is actually better.
I appreciate his help, however, it didn't filter the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.