【发布时间】:2013-09-02 13:07:36
【问题描述】:
我目前正在使用 csv 文件在我的 django 模型中插入数据。下面是一个正在使用的简单保存功能:
def save(self):
myfile = file.csv
data = csv.reader(myfile, delimiter=',', quotechar='"')
i=0
for row in data:
if i == 0:
i = i + 1
continue #skipping the header row
b=MyModel()
b.create_from_csv_row(row) # calls a method to save in models
该功能与 ascii 字符完美配合。但是,如果 csv 文件有一些非 ascii 字符,则会引发错误:UnicodeDecodeError “ascii”编解码器无法解码位置 1526 中的字节 0x93:序数不在范围内(128)
我的问题是:如何在保存我的 csv 文件之前删除非 ascii 字符以避免此错误。
提前致谢。
【问题讨论】:
标签: python django csv converter