Solution for: #31: Python csv and universal-newline mode
Use universal new-line mode to open the file first
- yliu on April 09, 2009, 05:35 PM UTC
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:
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.
>> 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:
PEP 278 -- Universal Newline Support
( http://www.python.org/dev/peps/pep-0278/ ) - found by yliu on April 09, 2009, 05:25 PM UTC
Comments
thx~ u re so helpful
— imox on October 16, 2010, 07:02 AM UTCnice.... thanks
— awais on January 30, 2012, 06:37 AM UTCThanks for ur help.Its working
— raghavendra on July 06, 2012, 05:26 AM UTCThis helped.
— Jitendra Singh Sisodiya on March 26, 2014, 05:16 PM UTCThanks a lot