question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Was trying to read an ods file and ran into UnboundLocalError in odfreader.py

See original GitHub issue
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

for file in os.listdir('data'): pandas.read_excel(pathlib.Path('data', file), engine='odf')

Sorry I don’t have a minimal data example at this time.

Problem description

Was trying to test pandas reading a collection of ods files and ran into this error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/util/_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 311, in read_excel
    return io.parse(
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 906, in parse
    return self._reader.parse(
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 443, in parse
    data = self.get_sheet_data(sheet, convert_float)
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_odfreader.py", line 91, in get_sheet_data
    value = self._get_cell_value(sheet_cell, convert_float)
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_odfreader.py", line 175, in _get_cell_value
    return self._get_cell_string_value(cell)
  File "/home/michael/.local/share/virtualenvs/merge-csv-NFbvYFrS/lib/python3.8/site-packages/pandas/io/excel/_odfreader.py", line 211, in _get_cell_string_value
    value.append(" " * spaces)
UnboundLocalError: local variable 'spaces' referenced before assignment

I took a look at the code in question and it seems like the line may be on the wrong indent level?

Expected Output

The usual dataframes 👍

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff2792bf16178d4e450fe7384244e50635733 python : 3.8.2.final.0 python-bits : 64 OS : Linux OS-release : 5.4.0-42-generic Version : #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.1.0 numpy : 1.19.1 pytz : 2020.1 dateutil : 2.8.1 pip : 20.0.2 setuptools : 44.0.0 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : 1.0.1 pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None fsspec : None fastparquet : None gcsfs : None matplotlib : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pytables : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None numba : None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
openwebcccommented, Aug 21, 2020

find a minimal .ods file here that has one column with a header and one data-cell with content Test (1) that causes the fail. Put the .ods in the same directory where you run the following python3 snippet:

python3

import pandas as pd
print(pd.show_versions())
df = pd.read_excel("bug_odfreader_l211_spaces.ods", sheet_name="Test123")

I think, the fail is related to the type of the data-cell where fragment.qname is reported as ('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'span'). As it is not handled by the if-clause, spaces is not set and value.append(" " * spaces) throws the error. Fixing the indent would solve the problem, but leave the span-clause unimplemented.

I could not reproduce creating a cell of type span in LibreOffice.

Sorry for linking to the .ods file but I was not able to upload it here as .ods is not a supported file type

0reactions
thisnamenotavailablecommented, Oct 7, 2020

Upgraded pandas to 1.1.3 and ran the program again.

No UnboundLocalError, so it seems to be working again.

thanks for the update.

On Wed, Oct 7, 2020 at 7:21 AM Asish Mahapatra notifications@github.com wrote:

@thisnamenotavailable https://github.com/thisnamenotavailable this is fixed in pandas v1.1.3 Can you check after upgrading?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pandas-dev/pandas/issues/35802#issuecomment-704699045, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARBNTZNJG7XC5VOXCBLVC3TSJP3GXANCNFSM4QE5CSMQ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

I don't know why I cannot import the .ods file - Stack Overflow
I wanted to deal with an excel data and prepared ods file and save in the directory where there is this python's file....
Read more >
automate the boring stuff with python 2nd edition
Project: Backing Up a Folder into a ZIP File ... Chapter 11: Debugging Shows how to use Python's various bug- finding and bug-fixing...
Read more >
https://huggingface.co/jeniya/BERTOverflow/commit/...
diff --git a/vocab.txt b/vocab.txt new file mode 100644 index ... +Spr +##umer +0x +member +creates +hit +operations +byte +Col +helps +##resh +Log +yes ......
Read more >
Al-Sweigart-Automate-The-Boring-Stuff-With-Python - StudyLib
You'll learn the basics of Python and explore Python's rich library of modules for performing specific tasks, like scraping data off websites, reading...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found