简体   繁体   English

“utf-8”编解码器无法解码位置 2912 中的字节 0xd5:在 Python 中读取 csv 文件时出现无效的连续字节错误

[英]'utf-8' codec can't decode byte 0xd5 in position 2912: invalid continuation byte Error when reading csv file in Python

I am cycling through the rows of a csv file, but come across this error when looping through the rows: 'utf-8' codec can't decode byte 0xd5 in position 2912: invalid continuation byte我正在循环浏览 csv 文件的行,但在循环浏览行时遇到此错误: 'utf-8' codec can't decode byte 0xd5 in position 2912: invalid continuation byte

I'm just trying to get the row count for the file with this function:我只是想用这个函数获取文件的行数:

def count_lines(filename):
    row_stored = ""
    try:
        with open(filename) as csvfile:
            data_reader = csv.reader(csvfile)
            next(data_reader)
            count = 0
            for index, row in enumerate(data_reader):
                if index == 1220119:
                    print(row)
                row_stored = row
                count += 1
            return count
    except Exception as e:
        print(f'There was a problem with your request: {e}\n', row_stored)
        return False

The row above the erroring row looks like this:错误行上方的行如下所示:

['817949019495', 'QMMZN1300568', '4/28/2017', 'Digital Revenue', 'Track', 'Download Europe', 'GB', 'Amazon International - UK', '', '2', '1.2126506333579932', '109926407', '2/28/2017']

And the row that throws the error looks like this:引发错误的行如下所示:

['817949019495', 'QMMZN1300568', '4/28/2017', 'Digital Revenue', 'Track', 'Download Europe', 'GB', 'Amazon International - UK', '', '2', '1.2126506333579932', '109926407', '2/28/2017']

I don't see any differences in the two.我看不出两者有什么区别。 Is there something with the formatting of this particular row that I'm not seeing?这个特定行的格式有什么我没有看到的吗?

Note: This csv file is 3.17 GB.注意:此 csv 文件为 3.17 GB。 Don't know if that's a contributing factor不知道这是否是一个促成因素

更改编码解决了这个问题

with open(filename, encoding="ISO-8859-1") as csvfile:

Here's evidence that it the encoding worked.这是编码有效的证据。 Still not sure what caused the error, but the data remained unaltered.仍然不确定是什么导致了错误,但数据保持不变。 MYSQL 截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取 python 中的文件时出现问题:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 168: invalid continuation byte - Problems read files in python:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 168: invalid continuation byte 当安装OSMnx时出现错误“ UnicodeDecodeError:'utf-8'编解码器无法解码位置24的字节0xd5:无效的连续字节” - when install OSMnx get erroe “UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 24: invalid continuation byte” UnicodeDecodeError:'utf-8'编解码器无法解码 position 0 中的字节 0xff:读取 csv 时 python 中的无效起始字节错误 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte error in python while reading a csv file UnicodeDecodeError when reading CSV file in Pandas with Python “'utf-8' codec can't decode byte 0xff in position 0: invalid start byte” - UnicodeDecodeError when reading CSV file in Pandas with Python “'utf-8' codec can't decode byte 0xff in position 0: invalid start byte” colab中的Python熊猫:UnicodeDecodeError:“ utf-8”编解码器无法解码位置0的字节0xd3:无效的继续字节UnicodeDecodeError: - Python Pandas in colab:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byteUnicodeDecodeError: Pandas read_excel UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: 无效的继续字节 - Pandas read_excel UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte Python Flask - UnicodeDecodeError:'utf-8'编解码器无法解码位置 0 中的字节 0xc4:无效的连续字节 - Python Flask - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte Python 错误:UnicodeDecodeError:'utf-8'编解码器无法解码 position 中的字节 0xde 1187:无效的继续字节 - Python Error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xde in position 1187: invalid continuation byte 'utf8'编解码器无法解码位置0的字节0xd0:无效的连续字节 - 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte Robot Framework 命令行错误 | 获取错误“utf8”编解码器无法解码位置 0 中的字节 0xd0:继续字节无效 - Robot Framework Command Line Error | Getting Error 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM