0

I'm using pandas to load an excel file. I get the file from an email attachment using my_excel_content = email.get_payload(decode=True) on the body of the email.

I would like to load my_excel_content into a pandas dataframe. I could write to it to file and use read_excel(path), but is there any way to load my excel in pandas directly instead of first saving it and loading it anew?

2 Answers 2

2

From the documentation of pandas :

Parameters iostr, bytes, ExcelFile, xlrd.Book, path object, or file-like object By file-like object, we refer to objects with a read() method, such as a file handler (e.g. via builtin open function) or StringIO.

So something like this should work :

from io import BytesIO
data = BytesIO(variable) #variable is supposed to be bytes
df = pd.read_excel(data)
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you can use StringIO:

from io import StringIO
import pandas as pd

attachment_content = email.get_payload(decode=True)

s = str(attachment_content, 'utf-8')
df = pd.read_csv(StringIO(s))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.