'How to send tabular list content in an email body python

I have a list which is a combination of splitted dataframe as below and I am trying to send teh content of it in an email body using email.message package. The string variable in the picture is what is passed to msg.set_content(text) function.

enter image description here

Unfortunately the result of this is not in the format as what I was expecting :

enter image description here

Any suggestion or quick fix ?



Solution 1:[1]

The Jupyter Notebook output can show the table nicely due to the use of a monospaced font. However, showing that string in a font that is not monospaced likely can't replicate the structure table as a is not the same width as a - character. I'd suggest looking at getting the output as a html table instead. A suggestion can be found here: Pandas send email containing dataframe as a visual table. In particular, as you are using pandas, you can export the table as HTML using df.to_html(), see https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_html.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 davidverweij