UnicodeDecodeError:'charmap'编解码器无法解码字节

时间:2016-08-29 10:45:25

标签: python-3.x encoding character-encoding web-scraping

我从网站上删除了信息,现在我正在尝试将其从.txt文件转移到excel。但是我一直收到这个错误:

 Traceback (most recent call last):`enter code here`
File "C:/Python/0 Projects/NDB/src/SortData.py", line 26, in <module>
for i, line in enumerate(fp):
File "C:\Python\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 3903: character maps to <undefined>
Exception ignored in: <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x00A9CD50>>
Traceback (most recent call last):
File "C:\Python\lib\site-packages\xlsxwriter\workbook.py", line 148, in __del__
Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.

您对如何克服此问题有任何提示吗?我是Python的初学者,所以感谢你让它保持超级简单:)这是我的代码:

worksheet = workbook.add_worksheet()

file = open("NDB.txt", 'r', encoding='utf-8')

def find_value(variable):
variable = line.find(variable)
begin = line.find(":", variable)
begin = begin + 1
end = line.find(',"', variable)
found_value = line[begin:end]
found_value2 = found_value.replace('"', "")
#found_value2 = found_value2.encode("utf-8")
return found_value2

 variables = ['code"', "side_code", "faction_code"]

 count = 0

 with open("NDB.txt") as fp:
   for i, line in enumerate(fp):
    while len(variables) > count:
        if (line.find(variables[count]) != -1):
            print(i)
            found_value2 = find_value(variables[count])
            worksheet.write(i, count, found_value2)
            count = count + 1
            # print(i, count, found_value)
        else:
            count = count + 1
count = 0

 workbook.close()

0 个答案:

没有答案