How to send correctly a table by email in python?

2019-09-16 09:51发布

i trying to send a ASCII table by email but when i received i got a unexpected format

the format that shows in my python script is

from tabulate import tabulate
message_launch = ['value','value2','value3','value4','value5','value6']
headers = ['data_a','data_b','data_c','data_d','date_e','data_f']
**t = tabulate(message_launch, headers=message_headers, missingval='?', stralign='center', tablefmt='grid').encode('utf-8')**

(Pdb) type(t)
<type 'str'>

+------------+----------+------------+----------+----------------+--------------+ | data_a | data_b | data_c | data_d | data_e | data_f | +============+==========+============+==========+================+==============+ | value1 | value2 | value3 | value4 | value5 | value6 | +------------+----------+------------+----------+----------------+--------------+-+

and the table that i got from my email is very disordered how can i receive in the correct the way the table by email ?

1条回答
2楼-- · 2019-09-16 10:35

Most email clients support html rendering. I think that would be the easiest way.

Pass 'html' to tablefmt:

t = tabulate(message_launch, headers=message_headers, missingval='?', stralign='center', tablefmt='html').encode('utf-8')

Then send t as html with whatever email library you are using.

查看更多
登录 后发表回答