Search code examples
pythonpandasobjectbytebytesio

How do I wrap a byte string in a BytesIO object using Python?


I'm writing a script with the Pandas library that involves reading the contents of an excel file.

The line currently looks like this:

test = pd.read_excel(archive_contents['spreadsheet.xlsx'])

The script works as intended with no issues, but I get a future warning depicting the following:

FutureWarning: Passing bytes to 'read_excel' is deprecated and will be removed in a future version. To read from a byte string, wrap it in a `BytesIO` object.
  test = pd.read_excel(archive_contents['spreadsheet.xlsx'])

In the interest of future proofing my code, how would I go about doing that?


Solution

  • As user459827 has commented, this will do the trick:

    from io import BytesIO
    
    test = pd.read_excel(BytesIO(archive_contents['spreadsheet.xlsx']))