#31: Python csv and universal-newline mode

Solved!
When using Python 2.5's csv module, I keep running into a "new-line character seen in unquoted field" error.
In [16]: f = csv.reader(open("/Users/yliu/foo/data", "rb"), dialect=csv.excel_tab) In [17]: for row in f: print row ....: ....: --------------------------------------------------------------------------- Error Traceback (most recent call last) /Users/yliu/Projects/foo/<ipython console=""> in <module>() Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Use universal new-line mode to open the file first

55
Turns out if you open the file descriptor in universal new-line mode, the csv module is far less brittle with handling empty cells in Excel documents:

>> f = csv.reader(open("/Users/yliu/foo/data", "rU"), dialect=csv.excel_tab) >> for row in f: print row

Notice the file flag: "U". This should fix any CSV parsing issues when your Excel sheet is irregularly formatted and populated. Check the reference for an explanation as to what exactly is universal-newline mode.

The error message is exactly what it says on the tin.

References used:

Comments

  1. thx~ u re so helpful

    imox on October 16, 2010, 07:02 AM UTC
  2. nice.... thanks

    awais on January 30, 2012, 06:37 AM UTC
  3. Thanks for ur help.Its working

    raghavendra on July 06, 2012, 05:26 AM UTC
  4. This helped.
    Thanks a lot

    Jitendra Singh Sisodiya on March 26, 2014, 05:16 PM UTC

Think you've got a better solution? Help 92049143cabb7ba896d7c06e19906303_small yliu out by posting your solution

PEP 278 -- Universal Newline Support

http://www.python.org/dev/peps/pep-0278/ - found by 92049143cabb7ba896d7c06e19906303_small yliu on April 09, 2009, 05:25 PM UTC

this link explains wtf is a universal-newline mode

Tags: python universal-newline newline