gpt4 book ai didi

python - csv 读取引发 "UnicodeDecodeError: ' charmap' 编解码器无法解码..."

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:46 25 4
gpt4 key购买 nike

我已经阅读了我能找到的所有帖子,但我的情况似乎很独特。我对 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:
...

关于python - csv 读取引发 "UnicodeDecodeError: ' charmap' 编解码器无法解码...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59082843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com