ValueError: Falling back to the ‘python‘ engine because the separator encoded in utf-8 is > 1 char l

本文讲述了在使用pandas读取csv文件时遇到的ValueError,原因在于文件中英文分号被误识别为中文分号。解决方案包括直接修改分号字符和指定engine='python'。关键词涉及pandas、csv读取、UTF-8编码和engine设置。

@创建于:20210407
@修改于:20210407

1、问题描述

python pandas 读csv文件遇到如下报错:

ValueError: Falling back to the ‘python’ engine because the separator encoded in utf-8 is > 1 char long, and the ‘c’ engine does not support such separators, but this causes ‘low_memory’ to be ignored as it is not supported by the ‘python’ engine.

2、解决办法

我的问题在于csv是英文分号,而在读的时候用的是中文分号。

if os.path.exists("data/test.csv"):
    df_day = pd.read_csv(filepath_or_buffer=
当在数据加载时出现 `'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte` 错误,意味着 UTF - 8 编码无法解析位于 15 至 16 位置的字节,这通常是编码不匹配导致的。以下是几种可行的解决方法: ### 更换编码方式 可以尝试使用其他编码方式打开文件,如 GBK、ISO - 8859 - 1。以 Python 为例,使用 ISO - 8859 - 1 编码打开文件: ```python with open('your_file.txt', 'r', encoding='ISO-8859-1') as f: data = f.read() ``` 这种方法在解析包含特定构造字符的脚本文件时可能有效,就像在解析 nessus 插件脚本内容时遇到类似错误,更换为 ISO - 8859 - 1 编码可以解决问题 [^3]。 ### 使用 chardet 库自动检测编码 使用 `chardet` 库可以自动检测文件的编码。以下是示例代码: ```python import chardet with open('your_file.txt', 'rb') as f: result = chardet.detect(f.read()) encoding = result['encoding'] with open('your_file.txt', 'r', encoding=encoding) as f: data = f.read() ``` 该方法通过读取文件的二进制内容,使用 `chardet.detect` 函数检测编码,然后使用检测到的编码打开文件 [^4]。 ### 忽略解码错误 在打开文件时,可以设置 `errors` 参数为 `'ignore'` 来忽略解码错误,但这种方法可能会丢失部分数据: ```python with open('your_file.txt', 'r', encoding='utf-8', errors='ignore') as f: data = f.read() ``` ### 替换解码错误的字符 可以设置 `errors` 参数为 `'replace'`,将无法解码的字符替换为 `?`: ```python with open('your_file.txt', 'r', encoding='utf-8', errors='replace') as f: data = f.read() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值