FYI, this post is out of date. As of Python 3.0, universal newline mode is now
the default and
'U' is accepted, but no longer does anything.
I deal with a lot of people that prefer to open reports in Microsoft Excel. I've gotten used to generating CSV (comma separated value) files in Python for these folks. Ever so often, someone sends me a CSV file they have created via Excel. Invariably, I will get the following error when trying to read that file with Python:
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
This is annoying, but the solution to the problem is to open the file with
universal newline mode enabled. This means using the mode
'rU' in your
open() function call. For example:
reader = csv.reader(open("data.csv", 'rU'), dialect='excel')
According to the
documentation, universal newline mode accepts
\r\n as valid