Please log into your user account or create a new one

Solution for: #31: Python csv and universal-newline mode

Use universal new-line mode to open the file first

30
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