Python-2.3b1 bugs on Windows2000 with: the new csv module,string replace, and the re module

Daniel Ortmann dortmann at lsil.com
Wed Jul 2 11:01:44 EDT 2003


Skip Montanaro <skip at pobox.com> writes:

Daniel> While using the csv module's DictWriter on MSDOS
Daniel> (a.k.a. Windows2000), the output files get newlines like
Daniel> \x0d\x0d\x0a instead of \x0d\x0a.

Daniel> csvwriter = csv.DictWriter( file( out1filename, 'w' ), infieldnames, extrasaction='ignore' )
Daniel> csvwriter.writerow( dict( zip( infieldnames, infieldnames ) ) )

Skip> CSV files are not really plain text files.  The line terminator
Skip> string is an explicit property of the file.  For example, you
Skip> might want to write a CSV file on a Windows 2000 machine which you
Skip> intend to read on a Mac OS9 system (where the line terminator is
Skip> just \r).  You need to open CSV files with the 'b' flag.  This
Skip> should work for you:

Skip> csvwriter = csv.DictWriter( file( out1filename, 'wb' ), infieldnames, extrasaction='ignore' )
Skip> csvwriter.writerow( dict( zip( infieldnames, infieldnames ) ) )

Ok, that is the same work around that I used.  Perhaps the documentation
should say something about using binary mode?

Or perhaps the DictWriter constructure should open the file in binary
mode if given a string rather than a file object?

How do we avoid people stumbling as I did?

-- 
Daniel Ortmann, LSI Logic, 3425 40th Av NW, Suite 200, Rochester MN 55901
work: Daniel.Ortmann at lsil.com / 507.535.3861 / 63861 int / 8012.3861 gdds
home: ortmann at isl.net / 507.288.7732, 2414 30Av NW #D, Rochester MN 55901





More information about the Python-list mailing list