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

3 Answers 3

7

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

This is the exception that is raised by pandas.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
#Nordle any idea how to do that
And except Exception isn't catching it?
@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?
0

The answer is here

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

or simple

except pd.errors.ParserError  as text_exception:  

Comments

0

This is what worked for me:

from dateutil.parser import ParserError

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.