【发布时间】:2019-03-08 17:18:35
【问题描述】:
我有 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
【问题讨论】:
-
我已经试过那个方法不起作用
-
好的,现在我需要 csv 文件来帮忙。
-
csv文件大小为1GB
-
尝试使用更小的东西 .. 请参阅下面的评论,您的终端是
cp1252并且在 csv 文件中是 utf-8 字符,这些字符在该终端中是不可打印的,现在您只有在打印。 -
您需要弄清楚使用哪种编码对文件进行编码。或者随机尝试other encodings。
'latin_1'或'iso8859_XX'变体可能有效,但这意味着您的文件中有控制字符。