Home > Mobile >  Unable to parse CSV files using pandas
Unable to parse CSV files using pandas

Time:02-01

What I am trying to do is parse a lot of csv files using a for loop and append it into a single sheet. But the error related to pandas package keeps coming up after it parses a few files.

for file in files:
   data = pd.read_csv(file,encoding='iso-8859-1')
   print(data)
...    

CSV File

Filesystem      Size  Used Avail Use% Mounted on
0          /dev/sda3       192G   24G  168G  13% /
1  /dev/sda1       512M  7.5M  505M   2% /boot/efi
  Filesystem      Size  Used Avail Use% Mounted on
0          /dev/sda1       200G  101G  100G  51% /
1       /dev/sdd1        50G   33M   50G   1% /u03
2        /dev/sdf1       400G   33M  400G   1% /wa
3       /dev/sde1       300G  230G   71G  77% /edi
4       /dev/sdb1        50G   33M   50G   1% /u01
5      /dev/sdg1        50G   11G   37G  22% /swap
6       /dev/sdc1        50G   33M   50G   1% /u02
  Filesystem      Size  Used Avail Use% Mounted on
0          /dev/sda1       200G   15G  186G   8% /
1       /dev/sdb1       300G  195G  106G  65% /u01
2   /dev/sdc1       100G   71G   30G  71% /opt/IBM
  Filesystem      Size  Used Avail Use% Mounted on
0          /dev/sda1       200G   14G  187G   7% /
1   /dev/sdb1       100G   66G   34G  66% /opt/IBM
2       /dev/sdc1       300G  158G  142G  53% /u01
  Filesystem      Size  Used Avail Use% Mounted on
0          /dev/sda1       200G  9.3G  191G   5% /
1       /dev/sdc1       100G   18G   83G  18% /u01
2   /dev/sdb1       150G   49G  102G  33% /opt/IBM

Output Error

Traceback (most recent call last):
  File "<pyshell#95>", line 2, in <module>
    data = pd.read_csv(file,encoding='iso-8859-1')
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\readers.py", line 605, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\readers.py", line 1442, in __init__
    self._engine = self._make_engine(f, self.engine)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\readers.py", line 1753, in _make_engine
    return mapping[engine](f, **self.options)
  File "C:\Users\102712\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 79, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 554, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

So far, I have tried specifying the encoding while reading the csv file, but nothing has changed.

So far, I have tried specifying the encoding while reading the csv file as I found it to be the most common cause of these type of problems but it still failed after reading the first few files.

The data is similar to the first few files that were able to be parsed.

CodePudding user response:

There is an empty file you are trying to parse.

import os
#
#
#
for file in files:
   if os.path.getsize(file) > 0:
       data = pd.read_csv(file,encoding='iso-8859-1')
       print(data)
  • Related