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?
#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.
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
Think you've got a better solution? Help yliu out by posting your solution
PEP 278 -- Universal Newline Support
http://www.python.org/dev/peps/pep-0278/ - found by yliu on April 09, 2009, 05:25 PM UTC
this link explains wtf is a universal-newline mode
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