Python捕获异常“ pandas.errors.ParserError:标记数据错误。C错误”

时间:2018-07-26 10:31:05

标签: python python-3.x pandas

我遇到了我的csv输入文件故障的整体阅读问题,可以通过在我的read_csv函数中添加“ error_bad_lines = False” 来解决这些问题。

但是我需要报告创建问题的许多文件,我以为我需要捕获该异常。我通过使用

进行了尝试
except pd.parser.CParserError 

except ExceptionSubclass as exceptionsubclass:

通过Internet搜索后,在两种情况下我都无法捕获此异常,如果您有任何举报所有故障文件的想法,请告诉我。

我遇到错误:

Traceback (most recent call last):
  File "main.py", line 134, in reading_csv
    df = pd.read_csv(absolute_path_of_file, sep=',', dtype=str, keep_default_na=False)
    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 966, in pandas._libs.parsers.TextReader._read_rows (pandas/_libs/parsers.c:11884)
  File "pandas/_libs/parsers.pyx", line 953, in pandas._libs.parsers.TextReader._tokenize_rows (pandas/_libs/parsers.c:11755)
  File "pandas/_libs/parsers.pyx", line 2184, in pandas._libs.parsers.raise_parser_error (pandas/_libs/parsers.c:28765)
pandas.errors.ParserError: Error tokenizing data. C error: Expected 7 fields in line 22, saw 8

1 个答案:

答案 0 :(得分:0)

尝试使用except pd.errors.ParserError代替except pd.parser.CParserError

这是大熊猫饲养的exception