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 'U'
or 'rU'
in your
open()
function call. For example:
reader = csv.reader(open("data.csv", 'rU'), dialect='excel')
According to the open()
documentation, universal newline mode accepts \n
, \r
, and \r\n
as valid
newline characters.
See also Python's Universal Newline Mode.