我试图设置一个电子邮件功能来发送格式化数据的电子邮件。我有以下工作可以在电子邮件中发送一个熊猫数据框,但用于将熊猫数据框转换为html的格式不会通过用户定义的空格数来发送。通过AWS SES与Python和boto3发送格式化的HTML邮件
#install the AWS SES SDK for Python (boto3) from the command line
#pip install boto3
import boto3
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) * 10000
def email(bodytext = 'No data...check your function arguments', dftoconvert = None):
region = 'us-west-2'
user = '' #insert your access key to use when creating the client
pw = '' #insert the secret key to use when creating the client
client = boto3.client(service_name = 'ses',
region_name = region,
aws_access_key_id = user,
aws_secret_access_key = pw)
me = '[email protected]'
you = ['[email protected]']
subject = 'testSUBJECT'
COMMASPACE = ', '
you = COMMASPACE.join(you)
#Build email message parts
#Build and send email
destination = { 'ToAddresses' : [you],
'CcAddresses' : [],
'BccAddresses' : []}
try:
bodyhtml = dftoconvert.to_html(float_format = lambda x: '({:15,.2f})'.format(abs(x)) if x < 0 else '+{:15,.2f}+'.format(abs(x)))
message = {'Subject' : {'Data' : subject},
'Body': {'Html' : {'Data' : bodyhtml}}}
except NoneType: #If there is no data to convert to html
message = {'Subject' : {'Data' : subject},
'Body': {'Text' : {'Data' : bodytext}}}
except Exception as e:
print(e)
result = client.send_email(Source = me,
Destination = destination,
Message = message)
return result if 'ErrorResponse' in result else ''
print(df)
email(dftoconvert = df)
由于某些原因,空格被忽略。我已将有效数字标识符更改为非常小且非常大,并且html输出看起来正确,并且Python中的浮点数的前导空格正确。见下文。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) * 10000
df
A B C D
0 1071.786803 -4354.776685 -3541.261466 2653.522461
1 489.865060 -12567.822512 13888.890274 14243.027303
2 471.995980 9473.174725 -1776.897694 5085.236174
3 5932.486256 -12558.720083 -17714.593696 -3503.574051
4 -8886.624311 -10286.622739 -4513.326771 2714.793954
5 5965.944055 10207.608141 19224.094501 4748.746867
6 14189.480430 -13826.251008 9847.711830 -1241.976560
7 -9044.406158 -14337.121541 19572.135090 -18146.356528
8 3057.233113 -14410.383480 -931.179975 -16273.711970
9 -14492.047676 -1150.506849 -1892.032700 -797.596310
df.to_html(float_format = lambda x: '({:15,.2f})'.format(abs(x)) if x < 0 else '+{:15,.2f}+'.format(abs(x)))
Out[3]: '
A | B | C | D | |
---|---|---|---|---|
0 | + 1,071.79+ | ( 4,354.78) | ( 3,541.26) | + 2,653.52+ |
1 | + 489.87+ | ( 12,567.82) | + 13,888.89+ | + 14,243.03+ |
2 | + 472.00+ | + 9,473.17+ | ( 1,776.90) | + 5,085.24+ |
3 | + 5,932.49+ | ( 12,558.72) | ( 17,714.59) | ( 3,503.57) |
4 | ( 8,886.62) | ( 10,286.62) | ( 4,513.33) | + 2,714.79+ |
5 | + 5,965.94+ | + 10,207.61+ | + 19,224.09+ | + 4,748.75+ |
6 | + 14,189.48+ | ( 13,826.25) | + 9,847.71+ | ( 1,241.98) |
7 | ( 9,044.41) | ( 14,337.12) | + 19,572.14+ | ( 18,146.36) |
8 | + 3,057.23+ | ( 14,410.38) | ( 931.18) | ( 16,273.71) |
9 | ( 14,492.05) | ( 1,150.51) | ( 1,892.03) | ( 797.60) |
但电子邮件似乎忽略任何前导空格和显示HTML同样的方式无论多少前导空格如何。我无法弄清楚如何使html格式显示在电子邮件中,就像html出现在Python输出中一样(使用前导空格)。有没有更好的办法?有没有比电子邮件格式化的数据框更好的格式?
的Python 3.5.2
熊猫0.18.1
numpy的1.11.1
+0
你能否澄清” ..没有在电子邮件未来通过如这应该”?它确实,但与您输入的内容不同?它根本没有?您可以使用[编辑]将其添加到您的问题。 –