python - 在python中加载大型csv文件“'utf-8'编解码器无法解密”时出错
问题描述
我有 1 GB 的 csv 文件,我无法读取该日志文件并在我的 csv 文件中的 python 和 pandas 代码中给出相同的错误,它不是超过一列的值,因为只有一个列值和所有我的 CSV 值是数字
with open("/Users/kiya/sep_sent.csv", encoding='utf-8') as f:
for i in f:
print(i.strip())
另一种方法:
with open("/Users/kiya/sep_sent.csv",encoding='cp1252') as f:
for i in f:
print(i.strip())
Traceback (most recent call last):
File "/Users/kiya/test8.py", line 5, in <module>
for i in f:
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 159: character maps to <undefined>
熊猫代码:
import pandas as pd
df = pd.read_csv("/Users/kiya/sep_sent.csv", encoding="utf-8")
print(df)
我的 csv 值如下:
0
0
0
....
5294751024
错误:
0
0
0
0
0
Traceback (most recent call last):
File "/Users/kiya//test8.py", line 4, in <module>
for i in f:
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 52: invalid start byte
解决方案
您也可以将编码参数传递给 read_csv
df = pd.read_csv("/Users/kiya/sep_sent.csv", encoding="utf-8")
推荐阅读
- nginx - 无法使用带有自定义错误页面的 modsecurity 发送审计日志
- iis - IIS ApplicationPool 无法正常启动且无错误
- javascript - 将父组件的转发引用传递给reactjs中的子组件
- ruby-on-rails - ActionCable 出于什么原因会部分停止工作并且仅在本地停止工作?
- c# - 无法从 C# 调用 Powershell 函数
- c# - 无法在 OpenTK 中显示三角形
- python - SIGKILL 出现在每次 dag 运行中。Airflow DAG 似乎缺乏运行资源
- angular - 在 Angular 8 中处理多个组件中的表单并在预览提交组件中显示错误
- java - 如何在 Java 中使用 IntVar 和 LinearExpression 实现 CPSat addDifferent
- flutter - Webview 错误(未处理的异常:在 dispose()之后调用 setState():_PlatformViewLinkState#a7b8e(生命周期状态:已失效,未安装))