【发布时间】:2011-10-07 07:17:48
【问题描述】:
我正在尝试访问 Django 中的 model.filefield 以使用 csv 模块解析 Python 中的 CSV 文件。它在 Windows 上运行,但在 Mac 上它给了我这个:
Exception Type: Error
Exception Value: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
这是代码:
myfile = customerbulk.objects.all()[0].fileup
mydata = csv.reader(myfile)
for email,mobile,name,civilid in mydata:
print email,mobile,name,civilid
【问题讨论】:
-
这是什么
customerbulk.objects.all()[0].fileup东西。它是模型上的文件名吗? -
fileup = models.FileField(verbose_name="CsvFile",upload_to='ExcelFiles') 如果我进行像 customerbulk.objects.get(pk=1) 这样的小查询
-
使用
rU的确切原因(与open()函数有关,不是csv特有的。):In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newlines support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'.docs.python.org/2/library/functions.html#open -
在 Python >= 3 中,使用
newline=''而不是mode='U'。
标签: django macos csv newline python-2.x