[ python-Bugs-944890 ] csv writer bug on windows

SourceForge.net noreply at sourceforge.net
Mon May 23 03:32:04 CEST 2005


Bugs item #944890, was opened at 2004-04-29 16:06
Message generated for change (Comment added) made by montanaro
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=944890&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Brian Kelley (wc2so1)
Assigned to: Skip Montanaro (montanaro)
Summary: csv writer bug on windows

Initial Comment:
The excel dialect is set up to be
class excel(Dialect):
    delimiter = ','
    quotechar = '"'
    doublequote = True
    skipinitialspace = False
    lineterminator = '\r\n'
    quoting = QUOTE_MINIMAL
register_dialect("excel", excel)

However,  on the windows platform, the lineterminator
should be simply "\n" 

My suggested fix is:

class excel(Dialect):
    delimiter = ','
    quotechar = '"'
    doublequote = True
    skipinitialspace = False
    if sys.platform == "win32":
        lineterminator = '\n'
    else:
        lineterminator = '\r\n'
    quoting = QUOTE_MINIMAL

Which seems to work.  It could be that I'm missing
something, but the universal readlines doesn't appear
to work for writing files.  If this is a usage issue,
it probably should be a documentation fix.

----------------------------------------------------------------------

>Comment By: Skip Montanaro (montanaro)
Date: 2005-05-22 20:32

Message:
Logged In: YES 
user_id=44345

This should have been closed long ago.  The documentation
states that files need to be opened in binary mode.


----------------------------------------------------------------------

Comment By: Brian Kelley (wc2so1)
Date: 2004-06-05 15:22

Message:
Logged In: YES 
user_id=424987

The example in the documentation fails...

import csv
writer = csv.writer(file("some.csv", "w"))
for row in someiterable:
    writer.writerow(row)

As I suspected, the fix is a documentation issue.  I will
make a documentation patch next week.  It will be my first
one :)

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2004-06-05 11:14

Message:
Logged In: YES 
user_id=31435

Excel on Windows puts \r\n line ends in .csv files it creates (I 
just tried it).  Since the OP mentioned "universal readlines", I 
bet he's opening the file with "U" (but it needs to be "rb").

----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2004-06-05 11:04

Message:
Logged In: YES 
user_id=44345

Can you attach an example that fails?  I don't have access
to Windows.  Note that you must open the file with binary
mode ("wb" or "rb").


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=944890&group_id=5470


More information about the Python-bugs-list mailing list