多茶树
2020-01-13 15:23
采纳率: 100%
浏览 1.7k
已采纳

python处理csv文件的编码格式问题

#csv模块中的DictReader函数中编码的问题

    处理日常的csv文件筛选问题中,通过以下的一段小代码,发现不同的编码格式下的程序有不同的错误,如下:

    #encoding=utf-8
    import csv

    with open('3020100_2019_qb.csv','r') as f:
            f_field = csv.reader(f,delimiter = '|')
            fieldnames = next(f_field)
            print(fieldnames)
            f_read = csv.DictReader(f,delimiter = '|',fieldnames=fieldnames)
            a = []
            for row in f_read:
                    d = {}
                    for k , v in row.items():
                            d[k] = v
                    a.append(d)
            print(a)


            with open('1211.csv','wt') as f:
                    writer = csv.DictWriter(f,fieldnames=fieldnames)
                    writer.writeheader()

                    for i in a :
                            #if i['    '] == '    ':
                                    writer.writerow(i)



错误提示(开头修改为#encoding=gbk也是同样的报错):
Traceback (most recent call last):
    File "E:/pycharm_file/csv_handle.py", line 10, in <module>
                for row in f_read:
    File "E:\anaconda\lib\csv.py", line 112, in __next__
            row = next(self.reader)
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 4781: illegal multibyte sequence
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题