使用 CSV Django 模块以通用换行模式打开文件 [英] Open the file in universal-newline mode using the CSV Django module

查看:17
本文介绍了使用 CSV Django 模块以通用换行模式打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问 Django 中的 model.filefield 以解析 CSV 文件在 Python 中使用 csv 模块.它在 Windows 上运行,但在 Mac 上它给了我这个:

I am trying to access a model.filefield in Django to parse a CSV file in Python using the csv module. It's working on Windows, but on Mac it gave me this:

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

推荐答案

我终于找到了解决方案:

I finally found the solution:

mypath = customerbulk.objects.get(pk=1).fileup.path
o = open(mypath,'rU')
mydata = csv.reader(o)

这篇关于使用 CSV Django 模块以通用换行模式打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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