Problems with extra blank line when using csv.writer in Python 3.1

Terry Reedy tjreedy at udel.edu
Tue Apr 27 12:43:43 EDT 2010


On 4/27/2010 9:05 AM, tkpmep at hotmail.com wrote:
> I’m experiencing a problem with the csv module in Python 3.1.2, and
> would greatly appreciate any help anyone can offer me. When writing
> csv files in Python 2.6, I open the output file as 'wb' to prevent a
> blank line being inserted after every line. Works like a charm. But I
> get an error if I do the same in 3.1; i.e if type the following
> sequence of commands:
>>>> import csv
>>>> outfile = open('c:/temp/test.csv', 'wb')
>>>> writer = csv.writer(outfile)
>>>> line = [1, 2, 3, 4]
>>>> writer.writerow(line)
> Traceback (most recent call last):
>    File "<pyshell#26>", line 1, in<module>
>      writer.writerow(line)
> TypeError: must be bytes or buffer, not str

In 3.x, t vs b modes actually mean to read/write text (str (unicode)) vs 
binary (bytes, for instance) objects.





More information about the Python-list mailing list