This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: csv.writer fails when within csv.reader
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezzieyguywuf, josh.r, martin.panter, r.david.murray, skip.montanaro
Priority: normal Keywords:

Created on 2015-06-24 20:05 by ezzieyguywuf, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg245772 - (view) Author: Wolfgang E. Sanyer (ezzieyguywuf) Date: 2015-06-24 20:05
I have a use case where I am using a csv.reader to loop through one file and trying to use a csv.writer to output to another file. However, when I do this, the csv.writer instance does not write anything to the output file.

ex:

import csv

with open("input.csv", "rb") as input, open("output.csv", "wb") as output:
    reader = csv.reader(input)
    writer = csv.writer(output)

    for row in reader:
        writer.writerow("data") # this writes out nothing
    
    writer.writerow("more data") # this writes out something
msg245785 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-24 23:22
It’s working properly for me in 2.7.10:

$ cat -A input.csv
a,b,c$
1,2,3$
$ python2 testcase.py
$ cat -A output.csv
d,a,t,a^M$
d,a,t,a^M$
m,o,r,e, ,d,a,t,a^M$
msg245788 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-06-25 01:32
Obvious possibility: input.csv is empty, so the loop never executes. You could always add prints within the loop as well, so you know it actually read something.
msg245808 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2015-06-25 12:48
@ezzieyguywuf - Can you provide an example of a non-empty input.csv which fails for you? Otherwise, I'd have to agree with @josh.r and @vadmium that it's working as it should.
msg247129 - (view) Author: Wolfgang E. Sanyer (ezzieyguywuf) Date: 2015-07-22 15:54
You can close this - it turns out that I was looping through the input file once first, and did not properly rewind it after doing so.

Might it make sense to have the csv module rewind the file after it has been looped through, so that it acts as a typical list?
msg247138 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-07-22 17:18
No, the object is just a wrapper around an iterator.  It doesn't know or care that you've passed in a file iterator...it is the file iterator's behavior that is non standard (this has been discussed elsewhere in the tracker, but it is not something that can be changed at this point, since files have always worked that way).
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68691
2015-07-22 17:18:45r.david.murraysetnosy: + r.david.murray
messages: + msg247138

resolution: not a bug
stage: test needed -> resolved
2015-07-22 15:54:37ezzieyguywufsetstatus: open -> closed

messages: + msg247129
2015-06-25 12:48:45skip.montanarosetnosy: + skip.montanaro
messages: + msg245808
2015-06-25 01:32:59josh.rsetnosy: + josh.r
messages: + msg245788
2015-06-24 23:22:52martin.pantersetnosy: + martin.panter

messages: + msg245785
stage: test needed
2015-06-24 20:05:15ezzieyguywufcreate