如何修复Python中的UnicodeDecodeError: 'utf-8'解码错误?
解决CSV文件UnicodeDecodeError问题
错误详情
运行邮箱验证工具时出现解码错误:
File "C:\Users\vk662\OneDrive - ST\Skrivebord\test\email_check.py", line 70, in <module> for row in csv_reader: File "C:\Program Files\Python310\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 4: invalid continuation byte
对应代码片段(第70行附近):
email_list = [] with open('email_in/test.csv', 'r', encoding='utf-8') as read_obj: csv_reader = csv.reader(read_obj, delimiter=';') for row in csv_reader: if (row): result = email_check(row[0],email_list) if result["Email ok"]: email_list.append(row[0]) if result["Email ok"]: email_ok.append(row[0]) else: str = "~~" for x, y in result.items(): if y: str += x + "~~" if x == "Duplicate email" and y: if row[0] in email_ok: email_ok.remove(row[0]) email_error.append(row[0] + str)
解决方法
1. 更换文件编码读取
错误中的0xe5字节是单字节编码(如Windows-1252/ISO-8859-1)里的å字符,说明CSV文件并非UTF-8编码。将open函数的encoding参数替换为对应编码即可:
# 优先尝试Windows常用的cp1252编码 with open('email_in/test.csv', 'r', encoding='cp1252') as read_obj:
或者用兼容所有单字节字符的latin-1:
with open('email_in/test.csv', 'r', encoding='latin-1') as read_obj:
2. 临时跳过错误字符(不推荐)
若不确定文件编码,可添加错误处理参数跳过或替换无效字符,但可能导致数据丢失:
# 用?替换无效字符 with open('email_in/test.csv', 'r', encoding='utf-8', errors='replace') as read_obj: # 或者直接忽略无效字符 # with open('email_in/test.csv', 'r', encoding='utf-8', errors='ignore') as read_obj:
3. 重新保存CSV为UTF-8
用Excel或文本编辑器(如Notepad++)打开CSV文件,选择UTF-8编码重新保存,之后即可用原代码的utf-8编码正常读取。
内容的提问来源于stack exchange,提问作者sujeeban
相关产品推荐
相关产品推荐

