我试图访问model.filefieldDjango 中使用该模块解析Python中的CSV文件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?
Run Code Online (Sandbox Code Playgroud)
这是代码:
myfile = customerbulk.objects.all()[0].fileup
mydata = csv.reader(myfile)
for email,mobile,name,civilid in mydata:
print email,mobile,name,civilid
Run Code Online (Sandbox Code Playgroud) 我怀疑这是一个常见的问题,但我似乎找不到答案.我试图从csv文件中删除所有逗号并用冒号替换它们.我通常会使用sed或vi,但我需要使用纯粹的python实现.这是我到目前为止提出的:
import csv
with open("temp.csv", mode="rU") as infile:
reader = csv.reader(infile, dialect="excel")
with open("temp2.txt", mode="w") as outfile:
writer = csv.writer(outfile)
for rows in reader:
for parsed_item in rows:
parsed_item = rows.replace(',', ':') # I can't do this with a list!
writer.writerow(parsed_item)
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗?在此先感谢您的帮助.