CSV读取错误:在未加引号的字段中看到换行符 [英] CSV read error: new-line character seen in unquoted field

查看:1089
本文介绍了CSV读取错误:在未加引号的字段中看到换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个python脚本,该脚本可用于包含10条记录的测试CSV数据集.当我将其扩展到实际数据集(几千行)时,出现以下错误:

I created a python script which works with a test CSV dataset of 10 records. When I scaled this up to the actual datasets (a few thousand rows), I am getting the following error:

_csv.错误:在未加引号的字段中出现换行符-您是否需要在通用换行模式下打开文件?

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

代码如下:

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读取方法是否可能存在缩放问题?

Perhaps there is a scale problem with the CSV reading method I am using?

推荐答案

来自 PEP- 0278 :

在具有通用换行符支持的Python中,open()的mode参数 也可以是"U",意思是打开以输入为具有通用性的文本文件 换行解释".模式"rU"也允许,为了与

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". Mode "rU" is also allowed, for symmetry with "rb"

所以尝试改变

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

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

如果错误仍然存​​在,请更改为

If the error persists, change to

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


根据Martijn Pieters的评论进行编辑.


Edited accorded to Martijn Pieters's comment.

这篇关于CSV读取错误:在未加引号的字段中看到换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆