小编Njo*_*bau的帖子

删除csv文件中的非ascii字符

我目前正在使用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
Run Code Online (Sandbox Code Playgroud)

该功能与ascii字符完美配合.但是,如果csv文件有一些非ascii字符,则会引发错误:UnicodeDecodeError'ascii'编解码器无法解码位置1526中的字节0x93:序号不在范围内(128)

我的问题是:如何在保存我的csv文件之前删除非ascii字符以避免此错误.

提前致谢.

python csv django converter

4
推荐指数
1
解决办法
9795
查看次数

标签 统计

converter ×1

csv ×1

django ×1

python ×1