我已经阅读了我能找到的所有帖子,但我的情况似乎很独特。我对 Python 完全陌生,所以这可能是基础的。我收到以下错误:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 70: character maps to undefined
当我运行代码时:
import csv
input_file = 'input.csv'
output_file = 'output.csv'
cols_to_remove = [4, 6, 8, 9, 10, 11,13, 14, 19, 20, 21, 22, 23, 24]
cols_to_remove = sorted(cols_to_remove, reverse=True)
row_count = 0 # Current amount of rows processed
with open(input_file, "r") as source:
reader = csv.reader(source)
with open(output_file, "w", newline='') as result:
writer = csv.writer(result)
for row in reader:
row_count += 1
print('\r{0}'.format(row_count), end='')
for col_index in cols_to_remove:
del row[col_index]
writer.writerow(row)
我做错了什么?
在 Python 3 中,csv 模块将文件作为 unicode 字符串处理,因此必须首先对输入文件进行解码。如果您知道的话,您可以使用确切的编码,或者只使用 Latin1,因为它将每个字节映射到具有相同代码点的 unicode 字符,以便解码+编码保持字节值不变。您的代码可能会变成:
...
with open(input_file, "r", encoding='Latin1') as source:
reader = csv.reader(source)
with open(output_file, "w", newline='', encoding='Latin1') as result:
...
我是一名优秀的程序员,十分优秀!