csv read引发“ UnicodeDecodeError:'charmap'编解码器无法解码...” [英] csv read raises "UnicodeDecodeError: 'charmap' codec can't decode..."

查看:120
本文介绍了csv read引发“ UnicodeDecodeError:'charmap'编解码器无法解码...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了所有可以找到的信息,但我的处境似乎很独特。我是Python的新手,所以这可能很基础。我收到以下错误:

I've read every post I can find, but my situation seems unique. I'm totally new to Python so this could be basic. I'm getting the following error:


UnicodeDecodeError:'charmap'编解码器无法解码位置70的字节0x8d:字符映射为未定义

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字符,以便解码+编码使字节值保持不变。您的代码可能变为:

In Python 3, the csv module processes the file as unicode strings, and because of that has to first decode the input file. You can use the exact encoding if you know it, or just use Latin1 because it maps every byte to the unicode character with same code point, so that decoding+encoding keep the byte values unchanged. Your code could become:

...
with open(input_file, "r", encoding='Latin1') as source:
    reader = csv.reader(source)
    with open(output_file, "w", newline='', encoding='Latin1') as result:
        ...

这篇关于csv read引发“ UnicodeDecodeError:'charmap'编解码器无法解码...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆