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

下面的代码工作到今天,当我从Windows机器导入,并得到这个错误:

在未加引号的字段中显示的新行字符 – 您是否需要以通用换行模式打开文件?

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] 

我该如何解决这个问题?

 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) 

很高兴看到csv文件本身,但这可能适合你,试试看,replace:

 file_read = csv.reader(self.file) 

有:

 file_read = csv.reader(self.file, dialect=csv.excel_tab) 

或者,用universal newline mode打开一个文件,并将其传递给csv.reader ,如下所示:

 reader = csv.reader(open(self.file, 'rU'), dialect=csv.excel_tab) 

或者,使用splitlines() ,如下所示:

 def read_file(self): with open(self.file, 'r') as f: data = [row for row in csv.reader(f.read().splitlines())] return data 

我意识到这是一个旧的post,但我遇到了同样的问题,没有看到正确的答案,所以我会试一试

Python错误:

 _csv.Error: new-line character seen in unquoted field 

尝试读取Macintosh(预OS X格式)CSV文件导致。 这些是使用CR作为行尾的文本文件。 如果使用MS Office,请确保您select纯CSV格式或CSV(MS-DOS)不要使用CSV(Macintosh)作为保存types。

我的首选EOL版本是LF(Unix / Linux / Apple),但我不认为MS Office提供了以这种格式保存的选项。

对于Mac OS X,请以“Windows逗号分隔(.csv)”格式保存您的CSV文件。

如果在Mac上发生这种情况(就像我这样做):

  1. 将文件另存为CSV (MS-DOS Comma-Separated)
  2. 运行以下脚本

     with open(csv_filename, 'rU') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: print ', '.join(row) 

尝试在你的windows导入的文件上运行dos2unix

这在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 

这是我遇到的错误。 我在MAC OSX中保存了.csv文件。

保存时,将其保存为“Windows逗号分隔值(.csv)”,以解决问题。