OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python pandas read_excel not catching exceptions

  • Thread starter Thread starter justinjt
  • Start date Start date
J

justinjt

Guest
EDIT: I have found that the reason my exceptions aren't catching is because they are actually being thrown by a different function to where I have put the try except block. Rookie mistake. If a mod would like to delete or suggest how I may edit the question please do. I haven't deleted it due to it having an answer.

I am trying to add some error handling to my code which reads data from an excel spreadsheet using the pandas library. I have not done this type of exception handling in Python before so not sure what I am doing wrong.

In my following snippet of code the FileNotFoundError exception works just fine when I put the xlsx file where my program can't find it. My program exits cleanly displaying the message and has the correct exit status.

Code:
try:
    xdf = pd.read_excel(xlsxdir + 'web_targets.xlsx', 'targets', skiprows=[0], index_col=None, na_values=['NA'], usecols = "A:F")
except KeyError as e:
    print("Expected column headers not found")
    sys.exit(1)
except TypeError as e:
    print("Type Error")
    sys.exit(1)
except FileNotFoundError as e:
    print("Excel file not found " + str(e))
    sys.exit(1)

Unfortunately when the program can find the file, but has an unexpected column key/heading neither the KeyError or ValueError exceptions catch and I get the following traceback:

Code:
Traceback (most recent call last):
  File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4410, in get_value
    return libindex.get_value_at(s, key)
  File "pandas/_libs/index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas/_libs/index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas/_libs/util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas/_libs/util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./create_new_configs.py", line 60, in <module>
    for record in xdfToDict(xdf):
  File "./create_new_configs.py", line 29, in xdfToDict
    'app': row['App'],
  File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4418, in get_value
    raise e1
  File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4404, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas/_libs/index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'App'

I initially didn't have the TypeError exception but I thought maybe since it is the first exception you must have to "catch" it first so I added it but nothing changed.

I have done some searching around and as far as I can tell I am doing the same as examples for e.g. KeyValue exceptions.

I would just like to be able to catch some potential data entry errors and return to the BASH script I will use to call this program with a suitable exit code.
<blockquote>
<p>EDIT: I have found that the reason my exceptions aren't catching is
because they are actually being thrown by a different function to
where I have put the try except block. Rookie mistake. If a mod would like to delete
or suggest how I may edit the question please do. I haven't deleted it due to it
having an answer.</p>
</blockquote>

<p>I am trying to add some error handling to my code which reads data from an excel spreadsheet using the <a href="https://pandas.pydata.org/" rel="nofollow noreferrer">pandas</a> library.
I have not done this type of exception handling in Python before so not sure what I am doing wrong.</p>

<p>In my following snippet of code the <code>FileNotFoundError</code> exception works just fine when I put the xlsx file where my program can't find it. My program exits cleanly displaying the message and has the correct exit status.</p>

<pre><code>try:
xdf = pd.read_excel(xlsxdir + 'web_targets.xlsx', 'targets', skiprows=[0], index_col=None, na_values=['NA'], usecols = "A:F")
except KeyError as e:
print("Expected column headers not found")
sys.exit(1)
except TypeError as e:
print("Type Error")
sys.exit(1)
except FileNotFoundError as e:
print("Excel file not found " + str(e))
sys.exit(1)
</code></pre>

<p>Unfortunately when the program can find the file, but has an unexpected column key/heading neither the <code>KeyError</code> or <code>ValueError</code> exceptions catch and I get the following traceback:</p>

<pre><code>Traceback (most recent call last):
File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4410, in get_value
return libindex.get_value_at(s, key)
File "pandas/_libs/index.pyx", line 44, in pandas._libs.index.get_value_at
File "pandas/_libs/index.pyx", line 45, in pandas._libs.index.get_value_at
File "pandas/_libs/util.pxd", line 98, in pandas._libs.util.get_value_at
File "pandas/_libs/util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./create_new_configs.py", line 60, in <module>
for record in xdfToDict(xdf):
File "./create_new_configs.py", line 29, in xdfToDict
'app': row['App'],
File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/series.py", line 871, in __getitem__
result = self.index.get_value(self, key)
File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4418, in get_value
raise e1
File "/home/justin/projects/web_targets/venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4404, in get_value
return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
File "pandas/_libs/index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'App'
</code></pre>

<p>I initially didn't have the <code>TypeError</code> exception but I thought maybe since it is the first exception you must have to "catch" it first so I added it but nothing changed.</p>

<p>I have done some searching around and as far as I can tell I am doing the same as examples for e.g. <code>KeyValue</code> exceptions.</p>

<p>I would just like to be able to catch some potential data entry errors and return to the BASH script I will use to call this program with a suitable exit code.</p>
 

Latest posts

D
Replies
0
Views
1
Dhanushka Amarakoon
D
S
Replies
0
Views
1
Shikhar Ambashta
S
Top