简体   繁体   English

CSV读取错误:在未加引号的字段中看到的换行符

[英]CSV read error: new-line character seen in unquoted field

I created a python script which works with a test CSV dataset of 10 records. 我创建了一个python脚本,它与10个记录的测试CSV数据集一起使用。 When I scaled this up to the actual datasets (a few thousand rows), I am getting the following error: 当我将其扩展到实际数据集(几千行)时,我收到以下错误:

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? _csv.Error:在未引用字段中看到的换行符 - 是否需要以通用换行模式打开文件?

The code is as follows: 代码如下:

with open('./Origins.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    origincoords = ['{Y},{X}'.format(**row) for row in reader]

The full error code is: 完整的错误代码是:

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?

Perhaps there is a scale problem with the CSV reading method I am using? 也许我正在使用的CSV读取方法存在规模问题?

From PEP-0278 : 来自PEP-0278

In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". 在具有通用换行符支持open()的Python中,mode参数也可以是“U”,意思是“打开输入作为具有通用换行符解释的文本文件”。 Mode "rU" is also allowed, for symmetry with "rb" 模式“rU”也是允许的,与“rb”对称

So try to change 所以试着改变

with open('./Destinations.csv', 'r') as csvfile:

to

with open('./Destinations.csv', 'rb') as csvfile:

If the error persists, change to 如果错误仍然存​​在,请更改为

with open('./Destinations.csv', 'rU') as csvfile:

Edited accorded to Martijn Pieters's comment. 根据Martijn Pieters的评论编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM