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

1.1k views Asked by At

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?

1

There are 1 answers

0
Learning is a mess On

Alternatively you could pass it a string, assuming decoding with utf-8 is enough:

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