Skip to content

Instantly share code, notes, and snippets.

@liamcryan
Created March 6, 2018 19:34
Show Gist options
  • Select an option

  • Save liamcryan/e4bfd16fa2b685c3f8d3af0e4f27bf43 to your computer and use it in GitHub Desktop.

Select an option

Save liamcryan/e4bfd16fa2b685c3f8d3af0e4f27bf43 to your computer and use it in GitHub Desktop.
working with byte strings in memory
# i read an excel file from gmail, and the response was returned as bytes like:
# b'this is my excel file as a byte string'
# i wanted to use pandas to get this into a dataframe using pd.get_excel(filename).
# typically I would input the path to the excel file, which would require me to
# save the excel file first using:
with open("my_file.xlsx", "wb") as f:
f.write()
# i found you don't have to save the file first though, you can pass a buffer directly into
# the input to get_excel. Google, "what is a buffer python" and you will see a buffer
# is a data structure that exposes binary data.
# so, the byte string needs to be put in a buffer somehow.
# Googling some more, i found the standard library io could do this, and this is how:
import io
excel_file_as_bytes = b'this is my excel file as a byte string'
excel_file_as_buffer = io.BytesIO(excel_file_as_bytes)
df = pd.read_excel(excel_file_as_buffer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment