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

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条回答 默认 最新

  • weixin_44336969
    'ゞBibo。 2020-01-14 10:14
    已采纳
        with open('3020100_2019_qb.csv', 'r', encoding='gbk', errors='ignore') as f:
            csv_reader = csv.reader(f)
            for row in csv_reader:
    
    点赞 评论
  • m0_37886429
    幸福丶如此 2020-01-13 15:49

    在开头指定字符编码# -*- coding:utf-8 -*-,在打开csv文件的时候也执行下字符编码encoding='utf-8'

    # -*- coding:utf-8 -*-
    
    import csv
    
    with open('3020100_2019_qb.csv','r',encoding='utf-8') 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',encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=fieldnames)
            writer.writeheader()
    
            for i in a:
                # if i['    '] == '    ':
                writer.writerow(i)
    
    点赞 评论
  • qq_39412061
    吃鸡王者 2020-01-14 14:56

    应该是在文件打开时指定文件的编码格式:with open('3020100_2019_qb.csv','r',encoding='utf8') as f:

    点赞 评论

相关推荐