Pandas: read_excel

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Purer_Einsteiger
User
Beiträge: 3
Registriert: Sonntag 21. März 2021, 10:24

Hallo Freunde,
ich versuche mittels Pandas und read_excel eine ods-Datei zu lesen. Lt Dokumentation sollte das ohne Probleme möglich sein und das Dateiformat auch automatisch erkannt werden. Einzig möglich ist es mir nur mit xls-Dateien.
Bei allen anderen wird folgender Fehler zurückgegeben.

Code: Alles auswählen

Traceback (most recent call last):
  File "/home/matt/Dropbox/TRG/Python/Versuch.py", line 3, in <module>
    tabelle = pandas.read_excel(io='Tabelle.ods')
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 163, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 206, in __init__
    self.book = xlrd.open_workbook(io)
  File "/home/matt/Dropbox/TRG/Python/xlrd/__init__.py", line 170, in open_workbook
    raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Openoffice.org ODS file; not supported
Ich habe sowohl die engine odf als auch ods versucht, mit dem gleichen Ergebnis.
Vermutlich eine einfache Lösung, aber...
Vielen Dank!
Matt
Benutzeravatar
__blackjack__
User
Beiträge: 13100
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Purer_Einsteiger: `engine` sollte den Wert "odf" haben, und dann sollte auch nicht *diese* Ausnahme kommen, denn dann dürfte `xlrd` gar nicht verwendet werden, wo *diese* Ausnahme ja ihren Ursprung hat.
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Purer_Einsteiger
User
Beiträge: 3
Registriert: Sonntag 21. März 2021, 10:24

Hallo blackjack,
danke für den Hinweis.
Wie gesagt, ich habe auch odf verwendet. In diesem Fall gibt es einen anderen Fehler, nämlich dass odf eine unbekannte engine ist.

Code: Alles auswählen

import pandas
xls = pandas.read_excel('Tabelle.xls')
print(xls)
ods = pandas.read_excel('Tabelle.ods', engine='odf')
print(ods)
Ergebnis:

Code: Alles auswählen

   km  Anzahl  Stunden
0   5       9      1.2
1   9       3      2.9
2   9      16      5.5
Traceback (most recent call last):
  File "/home/matthiasm/PythonTraining/versuch.py", line 6, in <module>
    ods = pandas.read_excel('Tabelle.ods', engine='odf')
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 163, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 199, in __init__
    raise ValueError("Unknown engine: %s" % engine)
ValueError: Unknown engine: odf
Die erste Datei wird also insoweit korrekt eingelesen.
Ich habe aber schon etwas weiter versucht: Wenn ich eine größere Datei, mit mehreren Sheets einlesen will, erhalte ich folgendes Problem:

Code: Alles auswählen

Traceback (most recent call last):
  File "/home/matthiasm/PythonTraining/versuch.py", line 10, in <module>
    datei = pandas.read_excel('TRG422.xls', sheet_name='PyExport1')
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 163, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 206, in __init__
    self.book = xlrd.open_workbook(io)
  File "/home/matthiasm/.local/lib/python3.5/site-packages/xlrd/__init__.py", line 182, in open_workbook
    ignore_workbook_corruption=ignore_workbook_corruption,
  File "/home/matthiasm/.local/lib/python3.5/site-packages/xlrd/book.py", line 104, in open_workbook_xls
    bk.parse_globals()
  File "/home/matthiasm/.local/lib/python3.5/site-packages/xlrd/book.py", line 1251, in parse_globals
    self.names_epilogue()
  File "/home/matthiasm/.local/lib/python3.5/site-packages/xlrd/book.py", line 1045, in names_epilogue
    evaluate_name_formula(self, nobj, namex, blah=blah)
  File "/home/matthiasm/.local/lib/python3.5/site-packages/xlrd/formula.py", line 1086, in evaluate_name_formula
    assert len(stack) >= nargs
AssertionError
Hängen diese beiden Probleme zusammen? Oder ist das wieder was anderes?
Danke!
Benutzeravatar
__blackjack__
User
Beiträge: 13100
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Purer_Einsteiger: Kann es sein, dass das alles ein bisschen sehr alt ist? Python 3.5 ist nicht mehr aktuell. Also nicht einfach nur ein bisschen alt, sondern da kommen weder Bug- noch Security-Fixes von den Python-Entwicklern. Und wenn Pandas bei `read_excel()` "odf" nicht als `engine` kennt, dann ist das wahrscheinlich auch uralt.
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Purer_Einsteiger
User
Beiträge: 3
Registriert: Sonntag 21. März 2021, 10:24

Hallo wieder!
Vielen Dank für den Hinweis... Ich verwendet Ubuntu 16.04 :roll: und habe jetzt mittels Anaconda auf die Python-Version 3.8 geupdatet. Und neben xls funktioniert jetzt auch xlsx. Aber mit ods-Dateien oder xls mit mehreren Arbeitsblättern habe ich weiterhin Schwierigkeiten.

Bei xls mit mehreren Arbeitsblättern erhalte ich folgenden Fehler:

Code: Alles auswählen

Traceback (most recent call last):
  File "versuch.py", line 10, in <module>
    datei = pandas.read_excel('TRG422.xls', sheet_name='PyExport1')
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1131, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 25, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 391, in __init__
    self.book = self.load_workbook(self.handles.handle)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 38, in load_workbook
    return open_workbook(file_contents=data)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/xlrd/__init__.py", line 172, in open_workbook
    bk = open_workbook_xls(
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/xlrd/book.py", line 104, in open_workbook_xls
    bk.parse_globals()
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/xlrd/book.py", line 1251, in parse_globals
    self.names_epilogue()
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/xlrd/book.py", line 1045, in names_epilogue
    evaluate_name_formula(self, nobj, namex, blah=blah)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/xlrd/formula.py", line 1086, in evaluate_name_formula
    assert len(stack) >= nargs
AssertionError
Hinsichtlich der odf-Dateien kommt folgender Fehler:

Code: Alles auswählen

Traceback (most recent call last):
  File "versuch.py", line 6, in <module>
    ods = pandas.read_excel('Tabelle.ods')
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1131, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/io/excel/_odfreader.py", line 30, in __init__
    import_optional_dependency("odf")
  File "/home/matthiasm/anaconda3/lib/python3.8/site-packages/pandas/compat/_optional.py", line 109, in import_optional_dependency
    raise ImportError(msg) from None
ImportError: Missing optional dependency 'odfpy'.  Use pip or conda to install odfpy.
Daraufhin hab ich mittel conda die Installation odfpy versucht, aber:

Code: Alles auswählen

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - odfpy -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0']
  - odfpy -> python[version='>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with your system:

  - feature:/linux-64::__glibc==2.23=0
  - python=3.8 -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']

Your installed version is: 2.23
Anscheinend hab ich auch hier eine falsche Version.

Wäre fantastisch, wenn sich für beides eine Lösung fände :wink:
Danke!
Antworten