[Python-Dev] Re: [Csv] csv module and universal newlines

Andrew McNamara andrewm at object-craft.com.au
Wed Jan 12 23:55:25 CET 2005


>You can argue that reading csv data from/writing csv data to a file on
>Windows if the file isn't opened in binary mode is an error.  Perhaps we
>should enforce that in situations where it matters.  Would this be a start?
>
>    terminators = {"darwin": "\r",
>                   "win32": "\r\n"}
>
>    if (dialect.lineterminator != terminators.get(sys.platform, "\n") and
>       "b" not in getattr(f, "mode", "b")):
>       raise IOError, ("%s not opened in binary mode" %
>                       getattr(f, "name", "???"))
>
>The elements of the postulated terminators dictionary may already exist
>somewhere within the sys or os modules (if not, perhaps they should be
>added).  The idea of the check is to enforce binary mode on those objects
>that support a mode if the desired line terminator doesn't match the
>platform's line terminator.

Where that falls down, I think, is where you want to read an alien
file - in fact, under unix, most of the CSV files I read use \r\n for
end-of-line.

Also, I *really* don't like the idea of looking for a mode attribute
on the supplied iterator - it feels like a layering violation. We've
advertised the fact that it's an iterator, so we shouldn't be using
anything but the iterator protocol.

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/


More information about the Csv mailing list