我创建了一个python脚本,它可以处理10个记录的测试CSV数据集。当我将其扩展到实际数据集(几千行)时,我收到以下错误:
_csv.Error:在不带引号的字段中看到的新行字符 - 您是否需要以通用换行模式打开文件?
代码如下:
with open('./Origins.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
origincoords = ['{Y},{X}'.format(**row) for row in reader]
完整的错误代码是:
Traceback (most recent call last):
File "./Driving.py", line 14, in <module>
origincoords = ['{Y},{X}'.format(**row) for row in reader]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 103, in next
self.fieldnames
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 90, in fieldnames
self._fieldnames = self.reader.next()
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
我正在使用的CSV阅读方法可能存在规模问题?
答案 0 :(得分:41)
来自PEP-0278:
在具有通用换行符支持的Python中,打开()模式参数 也可以是&#34; U&#34;,意思是&#34;打开输入作为具有通用的文本文件 换行解释&#34;。模式&#34; rU&#34;也是允许的,对称的 &#34; RB&#34;
所以尝试改变
with open('./Destinations.csv', 'r') as csvfile:
到
with open('./Destinations.csv', 'rb') as csvfile:
如果错误仍然存在,请更改为
with open('./Destinations.csv', 'rU') as csvfile:
根据Martijn Pieters的评论编辑。