简体   繁体   English

无法处理 python 中的 EmptyDataError

[英]Cannot handle EmptyDataError in python

I am trying to read in a huge csv file chunk by chunk using pandas read_csv function in combination with skiprows and nrows.我正在尝试使用 pandas read_csv function 结合 skiprows 和 nrows 逐块读取一个巨大的 csv 文件块。 I do this using a while loop.我使用 while 循环执行此操作。

When I reach the end of the file (skipping more rows than there are in the file) I get an EmptyDataError.当我到达文件末尾时(跳过的行多于文件中的行数),我得到一个 EmptyDataError。

This is no problem per se, I try handling this with a try - except.这本身没有问题,我试着用 try 来处理这个问题——除了。 However, the way I do it does not work...does anybody have an idea why?但是,我这样做的方式不起作用...有人知道为什么吗? I am a beginner when it comes to error handling.在错误处理方面,我是初学者。 This is my code:这是我的代码:

chunksize = 1000
inputfile = "testfile_with_five_rows.txt"
chunknumber = 0
while True:
    try: 
        data = pd.read_csv(inputfile, skiprows=chunksize*chunknumber, nrows=chunksize)
        print(data)
    except EmptyDataError:
        break

    chunknumber+=1

I get the following NameError, instead of handling the FileNotFound exception:我得到以下 NameError,而不是处理 FileNotFound 异常:

    ---------------------------------------------------------------------------
EmptyDataError                            Traceback (most recent call last)
<ipython-input-22-e3736135fc2f> in <module>()
      5     try:
----> 6         data = pd.read_csv(inputfile, skiprows=chunksize*chunknumber, nrows=chunksize)
      7         print(data)

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, doublequote, delim_whitespace, low_memory, memory_map, float_precision)
    677 
--> 678         return _read(filepath_or_buffer, kwds)
    679 

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    439     # Create the parser.
--> 440     parser = TextFileReader(filepath_or_buffer, **kwds)
    441 

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    786 
--> 787         self._make_engine(self.engine)
    788 

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1013         if engine == 'c':
-> 1014             self._engine = CParserWrapper(self.f, **self.options)
   1015         else:

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1707 
-> 1708         self._reader = parsers.TextReader(src, **kwds)
   1709 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

EmptyDataError: No columns to parse from file

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-22-e3736135fc2f> in <module>()
      6         data = pd.read_csv(inputfile, skiprows=chunksize*chunknumber, nrows=chunksize)
      7         print(data)
----> 8     except EmptyDataError:
      9         break
     10 

NameError: name 'EmptyDataError' is not defined

From comments:来自评论:

Have you imported EmptyDataError from pandas.errors?您是否从 pandas.errors 导入了 EmptyDataError? – heena bawa – 希娜巴瓦

from pandas.errors import EmptyDataError 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM