【发布时间】:2021-04-11 23:13:58
【问题描述】:
我正在解析一个 csv 文件,但出现以下错误
import os
import csv
from collections import defaultdict
demo_data = defaultdict(list)
if os.path.exists("infoed_daily _file.csv"):
f = open("infoed_daily _file.csv", "rt")
csv_reader = csv.DictReader(f)
line_no = 0
for line in csv_reader:
line_no +=1
print(line,line_no)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2483: character maps to
<undefined>
请指教。
谢谢.. -Prasanna.K
【问题讨论】:
-
可能文件使用的编码不同于
utf-8- 即latin-1、cp1250- 您可能必须在open()中使用它。您可以在 Google 中查看哪些 char 可能有代码0x81以及它可以是哪种编码。 -
当我运行
b'\x81'.decode('Latin1')或b'\x81'.decode('Latin2')或b'\x81'.decode('iso8859')或b'\x81'.decode('iso8859-2')时,它运行时不会出现错误 - 因此您的文件可以采用其中一些编码(或类似编码)并且您有使用它open(..., encoding='Latin1') -
谢谢大家。我用
encoding="latin-1"效果很好。非常感谢
标签: python