Python 捕获异常“pandas.errors.ParserError:标记数据时出错.C错误" [英] Python catch exception "pandas.errors.ParserError: Error tokenizing data. C error"

查看:68
本文介绍了Python 捕获异常“pandas.errors.ParserError:标记数据时出错.C错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取 csv 输入文件时遇到问题,我可以通过在 read_csv 函数中添加 "error_bad_lines=False" 来解决这个问题.

但是我需要报告这些造成问题的文件,我认为我需要捕获该异常.我通过使用

进行了尝试

除了 pd.parser.CParserError

except ExceptionSubclass 作为 exceptionsubclass:

通过互联网搜索后,在这两种情况下我都无法捕获此异常,如果您知道如何报告所有故障文件,请告诉我.

我遇到的错误:

回溯(最近一次调用最后一次):文件main.py",第 134 行,在 reading_csv 中df = pd.read_csv(absolute_path_of_file, sep=',', dtype=str, keep_default_na=False)数据 = self._reader.read(nrows)文件pandas/_libs/parsers.pyx",第 890 行,在 pandas._libs.parsers.TextReader.read (pandas/_libs/parsers.c:10862)文件pandas/_libs/parsers.pyx",第 912 行,在 pandas._libs.parsers.TextReader._read_low_memory (pandas/_libs/parsers.c:11138)文件pandas/_libs/parsers.pyx",第 966 行,在 pandas._libs.parsers.TextReader._read_rows (pandas/_libs/parsers.c:11884)文件pandas/_libs/parsers.pyx",第 953 行,在 pandas._libs.parsers.TextReader._tokenize_rows (pandas/_libs/parsers.c:11755)文件pandas/_libs/parsers.pyx",第 2184 行,在 pandas._libs.parsers.raise_parser_error (pandas/_libs/parsers.c:28765)pandas.errors.ParserError:标记数据时出错.C 错误:第 22 行预期有 7 个字段,看到 8 个

解决方案

尝试使用 except pd.errors.ParserError 而不是 except pd.parser.CParserError.>

这是异常 由熊猫饲养.

I am facing problem with my malfunction csv input file whole reading and which i can deal with by adding "error_bad_lines=False" in my read_csv func to remove those.

But i need to report these many files which is creating the problem, I assumed that i need to catch that exception. And i tried it via using

except pd.parser.CParserError 

and

except ExceptionSubclass as exceptionsubclass:

After searching over Internet, in both the cases i am not able to catch this exception, if you have any idea how to report all the malfunction file please let me know.

Error i am getting :

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

解决方案

Try using except pd.errors.ParserError instead of except pd.parser.CParserError.

This is the exception that is raised by pandas.

这篇关于Python 捕获异常“pandas.errors.ParserError:标记数据时出错.C错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆