Python: Error: new-line character – how to open the file in universal-newline mode ?

I was playing with the CSV module from the Python standard library. Typically using a code such as:

    import csv
    import zipfile
    zip = zipfile.ZipFile("file.zip")
    zipdata = zip.open("ExistingFile.csv")
    data_iter = csv.reader(zipdata, delimiter=",")
    data_iter.next()

So far so good, it did what I wanted that is reading a CSV file stored in a ZIP archive.

Then, I opened another ZIP archive and got this error:

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-19-d6c927ad0840> in <module>()
----> 1 data_iter.next()
Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

It appears that the archive was created under MacOSX and I work under Linux so I suspected an issue with the end-of-line convention that are different under different systems (e.g., \n under Linux, \r under Mac and \r\n under Windows).

The solution is to open the file in universal mode. Replace

zipdata = zip.open("ExistingFile.csv")  # default mode is "r"

by

zipdata = zip.open("ExistingFile.csv", "rU")
Please follow and like us:
This entry was posted in Python and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *