UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to

前端 未结 3 1060
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 16:57

I am student doing my master thesis. As part of my thesis, I am working with python. I am reading a log file of .csv format and writing the ext

相关标签:
3条回答
  • 2020-12-10 17:31
    with open('input.tsv','rb') as f:
    for ln in f:
        decoded=False
        line=''
        for cp in ('cp1252', 'cp850','utf-8','utf8'):
            try:
                line = ln.decode(cp)
                decoded=True
                break
            except UnicodeDecodeError:
                pass
        if decoded:
            # use 'line'
    
    0 讨论(0)
  • 2020-12-10 17:39

    i have solved this issue. we can use this code

    import codecs
    types_of_encoding = ["utf8", "cp1252"]
    for encoding_type in types_of_encoding:
        with codecs.open(filename, encoding = encoding_type, errors ='replace') as csvfile:
            your code
            ....
            ....
    
    0 讨论(0)
  • 2020-12-10 17:45

    I have solved this issue by simply adding a parameter in open()

    with open(filename, encoding = 'cp850') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
    
    0 讨论(0)
提交回复
热议问题