Python 在未引用的字段错误中看到的 CSV 换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17315635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSV new-line character seen in unquoted field error
提问by GrantU
the following code worked until today when I imported from a Windows machine and got this error:
以下代码一直工作到今天,当我从 Windows 机器导入并收到此错误时:
new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
在未加引号的字段中看到换行符 - 您是否需要以通用换行符模式打开文件?
import csv
class CSV:
def __init__(self, file=None):
self.file = file
def read_file(self):
data = []
file_read = csv.reader(self.file)
for row in file_read:
data.append(row)
return data
def get_row_count(self):
return len(self.read_file())
def get_column_count(self):
new_data = self.read_file()
return len(new_data[0])
def get_data(self, rows=1):
data = self.read_file()
return data[:rows]
How can I fix this issue?
我该如何解决这个问题?
def upload_configurator(request, id=None):
"""
A view that allows the user to configurator the uploaded CSV.
"""
upload = Upload.objects.get(id=id)
csvobject = CSV(upload.filepath)
upload.num_records = csvobject.get_row_count()
upload.num_columns = csvobject.get_column_count()
upload.save()
form = ConfiguratorForm()
row_count = csvobject.get_row_count()
colum_count = csvobject.get_column_count()
first_row = csvobject.get_data(rows=1)
first_two_rows = csvobject.get_data(rows=5)
采纳答案by alecxe
It'll be good to see the csv file itself, but this might work for you, give it a try, replace:
看到 csv 文件本身会很好,但这可能对你有用,试一试,替换:
file_read = csv.reader(self.file)
with:
和:
file_read = csv.reader(self.file, dialect=csv.excel_tab)
Or, open a file with universal newline mode
and pass it to csv.reader
, like:
或者,打开一个文件universal newline mode
并将其传递给csv.reader
,例如:
reader = csv.reader(open(self.file, 'rU'), dialect=csv.excel_tab)
Or, use splitlines()
, like this:
或者,splitlines()
像这样使用:
def read_file(self):
with open(self.file, 'r') as f:
data = [row for row in csv.reader(f.read().splitlines())]
return data
回答by rectummelancolique
Try to run dos2unix
on your windows imported files first
尝试先dos2unix
在 Windows 导入的文件上运行
回答by g.kovatchev
I realize this is an old post, but I ran into the same problem and don't see the correct answer so I will give it a try
我意识到这是一个旧帖子,但我遇到了同样的问题并且没有看到正确的答案,所以我会试一试
Python Error:
蟒蛇错误:
_csv.Error: new-line character seen in unquoted field
Caused by trying to read Macintosh (pre OS X formatted) CSV files. These are text files that use CR for end of line. If using MS Office make sure you select either plain CSVformat or CSV (MS-DOS). Do not use CSV (Macintosh)as save-as type.
由尝试读取 Macintosh(OS X 格式之前的)CSV 文件引起。这些是使用 CR 作为行尾的文本文件。如果使用 MS Office,请确保选择普通CSV格式或CSV (MS-DOS)。不要使用 CSV (Macintosh)作为另存为类型。
My preferred EOL version would be LF (Unix/Linux/Apple), but I don't think MS Office provides the option to save in this format.
我首选的 EOL 版本是 LF(Unix/Linux/Apple),但我认为 MS Office 不提供以这种格式保存的选项。
回答by BoltzmannBrain
For Mac OS X, save your CSV file in "Windows Comma Separated (.csv)" format.
对于 Mac OS X,请以“Windows 逗号分隔 (.csv)”格式保存 CSV 文件。
回答by Nimo
If this happens to you on mac(as it did to me):
如果你在 mac 上遇到这种情况(就像我遇到的那样):
- Save the file as
CSV (MS-DOS Comma-Separated)
Run the following script
with open(csv_filename, 'rU') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: print ', '.join(row)
- 将文件另存为
CSV (MS-DOS Comma-Separated)
运行以下脚本
with open(csv_filename, 'rU') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: print ', '.join(row)
回答by Resonance
This worked for me on OSX.
这在 OSX 上对我有用。
# allow variable to opened as files
from io import StringIO
# library to map other strange (accented) characters back into UTF-8
from unidecode import unidecode
# cleanse input file with Windows formating to plain UTF-8 string
with open(filename, 'rb') as fID:
uncleansedBytes = fID.read()
# decode the file using the correct encoding scheme
# (probably this old windows one)
uncleansedText = uncleansedBytes.decode('Windows-1252')
# replace carriage-returns with new-lines
cleansedText = uncleansedText.replace('\r', '\n')
# map any other non UTF-8 characters into UTF-8
asciiText = unidecode(cleansedText)
# read each line of the csv file and store as an array of dicts,
# use first line as field names for each dict.
reader = csv.DictReader(StringIO(cleansedText))
for line_entry in reader:
# do something with your read data
回答by Suraj
This is an error that I faced. I had saved .csv file in MAC OSX.
这是我面临的一个错误。我在 MAC OSX 中保存了 .csv 文件。
While saving, save it as "Windows Comma Separated Values (.csv)" which resolved the issue.
保存时,将其另存为“Windows 逗号分隔值 (.csv)”,从而解决了该问题。
回答by Dougyfresh
I know this has been answered for quite some time but not solve my problem. I am using DictReader and StringIO for my csv reading due to some other complications. I was able to solve problem more simply by replacing delimiters explicitly:
我知道这已经回答了很长一段时间,但没有解决我的问题。由于其他一些并发症,我正在使用 DictReader 和 StringIO 来读取 csv。通过显式替换分隔符,我能够更简单地解决问题:
with urllib.request.urlopen(q) as response:
raw_data = response.read()
encoding = response.info().get_content_charset('utf8')
data = raw_data.decode(encoding)
if '\r\n' not in data:
# proably a windows delimited thing...try to update it
data = data.replace('\r', '\r\n')
Might not be reasonable for enormous CSV files, but worked well for my use case.
对于巨大的 CSV 文件可能不合理,但对我的用例来说效果很好。
回答by p699
Alternative and fast solution : I faced the same error. I reopened the "wierd" csv file in GNUMERIC on my lubuntu machine and exported the file as csv file. This corrected the issue.
替代且快速的解决方案:我遇到了同样的错误。我在我的 lubuntu 机器上的 GNUMERIC 中重新打开了“wierd”csv 文件,并将该文件导出为 csv 文件。这纠正了这个问题。