Rendering csv data line-by-line without writing file
Question: I want to change a large CSV-file and write result into new file.

My python script `run.py`:

    
    
    import csv
    writer = csv.writer(open(..., 'w'))
    for l in csv.reader(open(...)):
        l[0] = 'foo' if l[1] else 'bar'
        writer.writerow(l)
    

I've realize that more suitable for me write file using Linux `pipe`:

    
    
    run.py > new-csv-file.csv
    

I've written:

    
    
    import csv
    import io
    s = io.StringIO()
    writer = csv.writer(s)
    for l in csv.reader(open(...)):
        l[0] = 'foo' if l[1] else 'bar'
        writer.writerow(l)
        print(s.getvalue())
    

It's wrong code. Each cycle appends new line in `s` and prints whole file
content on each cycle, not line-by-line.

How can I clean `s` on each cycle? Or how can I render csv-string line-by-
line?

    
    
    for l in csv.reader(open(...)):
        l[0] = 'foo' if l[1] else 'bar'
        s = my_imaginary_csv_render(l)
        print(s)
    


Answer: So as I understand you correct you want that your code, instead of file print
to stdout? Here solution:

    
    
    import csv
    
    for l in csv.reader(open(...)):
       l[0] = 'foo' if l[1] else 'bar'
       print(", ".join(l))
    

This solution not works as expected if data fields contain line separators,
then to not duplicate csv logic for escaping special symbols in csv you can
use:

    
    
    csv.writer(sys.stdout)
    

