6

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
Indrajeet Gour
  • 4,020
  • 5
  • 43
  • 70

3 Answers3

6

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

This is the exception that is raised by pandas.

Nordle
  • 2,915
  • 3
  • 16
  • 34
  • i used all the mentioned exception class "except pandas.errors.ParserError:" | "except pandas.errors.DtypeWarning:" | "except pandas.errors.EmptyDataError:" and others but unable to catch our scenario. – Indrajeet Gour Jul 26 '18 at 16:37
  • #Nordle any idea how to do that – Indrajeet Gour Jul 27 '18 at 04:25
  • 1
    And `except Exception` isn't catching it? – Nordle Jul 27 '18 at 05:43
  • 1
    @Nordle except Exception works, thanks! Was looking for the same solution to the same problem. :) Is there still no way to pinpoint a pandas ParserError non-generally without "except Exception" yet? – Vaidøtas I. Jul 27 '19 at 16:51
0

The answer is here

from pandas.errors import ParserError
try:
    ...
except ParserError:
    ...

or simple

except pd.errors.ParserError  as text_exception:  
Vasyl Kolomiets
  • 365
  • 8
  • 20
0

This is what worked for me:

from dateutil.parser import ParserError
Alaa M.
  • 4,961
  • 10
  • 54
  • 95