Solution for: #31: Python csv and 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