熊猫在一台计算机上引发ParserError,但在另一台计算机上未引发

时间:2018-11-28 23:29:54

标签: python python-3.x pandas

这是我的代码,在我朋友的计算机上可以很好地工作:

#!/usr/bin/python

import pandas as pd

df = pd.read_csv("report.csv")
df = df.drop("Agent Name", axis=1)
df.to_csv("agent_report_updated.csv")

这是我收到的错误:

Traceback (most recent call last):
  File "./agent_calls_report.py", line 10, in <module>
    df = pd.read_csv("report.csv")
  File "/usr/lib/python3.7/site-packages/pandas/io/parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/lib/python3.7/site-packages/pandas/io/parsers.py", line 446, in _read
    data = parser.read(nrows)
  File "/usr/lib/python3.7/site-packages/pandas/io/parsers.py", line 1036, in read
    ret = self._engine.read(nrows)
  File "/usr/lib/python3.7/site-packages/pandas/io/parsers.py", line 1848, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
  File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas/_libs/parsers.pyx", line 945, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 932, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas/_libs/parsers.pyx", line 2112, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 34 fields in line 3, saw 35

有人知道为什么它可以在一台计算机上而不是另一台计算机上工作吗?编辑:我已经确认我们正在使用相同版本的Python(3.7.1)和Pandas,唯一的区别是,当我在Linux上时,他有Mac。

1 个答案:

答案 0 :(得分:0)

我相信这是编码方面的问题

尝试:

import pandas as pd
df = pd.read_csv("report.csv",encoding='cp1252')
df = df.drop("Agent Name", axis=1)
df.to_csv("agent_report_updated.csv")

还有其他编码选项,您可以尝试使用utf-8而不是cp1252。 Here是使用的编码列表。