关于pandas读取csv文件时出现UnicodeDecodeError

错误1:UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb8 in position 0: invalid start byte

问题描述

运行如下代码时报错

UnicodeDecodeError                        Traceback (most recent call last)
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas\_libs\parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb8 in position 0: invalid start byte

解决方案

在read_csv函数中添加参数

encoding="gbk"

错误2:UnicodeDecodeError: ‘utf-8’ codec can’t decode bytes in position 42-43: invalid continuation byte

问题描述

UnicodeDecodeError                        Traceback (most recent call last)
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas\_libs\parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 42-43: invalid continuation byte

解决方案

在read_csv函数中添加参数

encoding='ISO-8859-1'

你可能感兴趣的:(ERROR,pandas,python,开发语言)