0

I found this example of a simple CSV read / write.

This code reads and writes the CSV file, sorting on the 7th column.

import csv
with open('input.csv', newline='') as csvfile:
    rdr = csv.reader(csvfile)
    l = sorted(rdr, key=lambda x: x[6], reverse=True)
with open('output.csv', 'w') as csvout:
    wrtr = csv.writer(csvout)
    wrtr.writerows(l)

The code above outputs a CSV file into output.csv

If I need to do some additional processing other than sorting, I might do it by iterating through the rows of the CSV object. The simplest example exhibiting the problem is just iterating the rows and printing them:

import csv
with open('input.csv', newline='') as csvfile:
    rdr = csv.reader(csvfile)
    for arow in rdr:
        print(arow)
    l = sorted(rdr, key=lambda x: x[6], reverse=True)
with open('output.csv', 'w') as csvout:
    wrtr = csv.writer(csvout)
    wrtr.writerows(l)

The code above prints the CSV rows to the screen as expected. However, the output.csv file is then empty ( 0 bytes)

Why does iteration through the rows cause no output from csv.writer? Is there some type of persistent state in the CSV object that has to be reset?

3
  • 3
    rdr is an iterator. You can only loop through an iterator once. Convert it to a list if you want to process it multiple times. Commented Jul 27, 2021 at 22:53
  • Thanks for fast response! I didn't realize rdr was an iterator. Adding mylist = list(rdr) and using mylist is the solution. Commented Jul 27, 2021 at 23:02
  • @tim11g: Please add this as a response and accept so that the question does not hang open. Commented Jul 28, 2021 at 11:56

1 Answer 1

0

As @Barmar correctly noted:

rdr is an iterator. You can only loop through an iterator once. Convert it to a list if you want to process it multiple times.

Working code:

import csv
with open('input.csv', newline='') as csvfile:
    rdr = csv.reader(csvfile)
    mylist = list(rdr)
    for arow in mylist:
        print(arow)
    l = sorted(mylist, key=lambda x: x[6], reverse=True)
with open('output.csv', 'w') as csvout:
    wrtr = csv.writer(csvout)
    wrtr.writerows(l)
Sign up to request clarification or add additional context in comments.

Comments

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.