尝试使用 Pandas 读取 csv 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46230117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error while trying to use pandas to read a csv
提问by anush95
import pandas
df = pandas.read_csv("trial.csv")
The above code is used to read a simple csv file. But I keep getting the following error
上面的代码用于读取一个简单的csv文件。但我不断收到以下错误
File "C:\Users\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1748, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 890, in pandas._libs.parsers.TextReader.read (pandas\_libs\parsers.c:10862)
File "pandas\_libs\parsers.pyx", line 912, in pandas._libs.parsers.TextReader._read_low_memory (pandas\_libs\parsers.c:11138)
File "pandas\_libs\parsers.pyx", line 989, in pandas._libs.parsers.TextReader._read_rows (pandas\_libs\parsers.c:12175)
File "pandas\_libs\parsers.pyx", line 1117, in pandas._libs.parsers.TextReader._convert_column_data (pandas\_libs\parsers.c:14136)
File "pandas\_libs\parsers.pyx", line 1169, in pandas._libs.parsers.TextReader._convert_tokens (pandas\_libs\parsers.c:14972)
File "pandas\_libs\parsers.pyx", line 1273, in pandas._libs.parsers.TextReader._convert_with_dtype (pandas\_libs\parsers.c:17119)
File "pandas\_libs\parsers.pyx", line 1289, in pandas._libs.parsers.TextReader._string_convert (pandas\_libs\parsers.c:17347)
File "pandas\_libs\parsers.pyx", line 1524, in pandas._libs.parsers._string_box_utf8 (pandas\_libs\parsers.c:23041)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 43: invalid continuation byte
回答by Dayantat
Hi sorry I am so late to this, please change your code to the below and see if that works.
嗨,对不起,我这么晚了,请将您的代码更改为下面的代码,看看是否有效。
import pandas
df = pandas.read_csv("trial.csv", encoding="ISO-8859-1")
回答by warkitty
import pandas
df = pandas.read_csv("trial.csv", "rb")
if none of the suggestions above worked, "rb" read binary might do the trick
如果上述建议均无效,则“rb”读取二进制文件可能会起作用
回答by Danny_ds
Your parser is trying to parse utf-8
data, but your file seems to be in another encoding (or there could just be an invalid character).
您的解析器正在尝试解析utf-8
数据,但您的文件似乎采用另一种编码(或者可能只是一个无效字符)。
Try to instruct the parser to parse as plain ascii
, perhaps with some codepage (I don't know Python, so can't help with that).
尝试指示解析器解析为plain ascii
,也许使用一些代码页(我不懂 Python,所以无能为力)。
Looks like you need to use the encoding
parameter.
看起来您需要使用该encoding
参数。
Here is the list with possible encodings.
这是可能的编码列表。