형식이 지정된 데이터로 전자 메일을 보내도록 전자 메일 기능을 설정하려고합니다. 나는 팬더 데이터 프레임을 이메일로 보내기 위해 작동하는 다음을 가지고 있지만 팬더 데이터 프레임을 HTML로 변환하는 데 사용되는 형식은 사용자가 정의한 공백 개수의 전자 메일을 통해 전달되지 않습니다.AWS SES를 통해 형식화 된 html 전자 메일을 Python 및 boto3과 함께 보냅니다.
#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 출력이 파이썬의 수레에 대한 올바른 선행 공백 수와 정확하게 일치합니다. 아래를 참조하십시오.
Out[3]: '<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>A</th>\n <th>B</th>\n <th>C</th>\n <th>D</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>+ 1,071.79+</td>\n <td>( 4,354.78)</td>\n <td>( 3,541.26)</td>\n <td>+ 2,653.52+</td>\n </tr>\n <tr>\n <th>1</th>\n <td>+ 489.87+</td>\n <td>( 12,567.82)</td>\n <td>+ 13,888.89+</td>\n <td>+ 14,243.03+</td>\n </tr>\n <tr>\n <th>2</th>\n <td>+ 472.00+</td>\n <td>+ 9,473.17+</td>\n <td>( 1,776.90)</td>\n <td>+ 5,085.24+</td>\n </tr>\n <tr>\n <th>3</th>\n <td>+ 5,932.49+</td>\n <td>( 12,558.72)</td>\n <td>( 17,714.59)</td>\n <td>( 3,503.57)</td>\n </tr>\n <tr>\n <th>4</th>\n <td>( 8,886.62)</td>\n <td>( 10,286.62)</td>\n <td>( 4,513.33)</td>\n <td>+ 2,714.79+</td>\n </tr>\n <tr>\n <th>5</th>\n <td>+ 5,965.94+</td>\n <td>+ 10,207.61+</td>\n <td>+ 19,224.09+</td>\n <td>+ 4,748.75+</td>\n </tr>\n <tr>\n <th>6</th>\n <td>+ 14,189.48+</td>\n <td>( 13,826.25)</td>\n <td>+ 9,847.71+</td>\n <td>( 1,241.98)</td>\n </tr>\n <tr>\n <th>7</th>\n <td>( 9,044.41)</td>\n <td>( 14,337.12)</td>\n <td>+ 19,572.14+</td>\n <td>( 18,146.36)</td>\n </tr>\n <tr>\n <th>8</th>\n <td>+ 3,057.23+</td>\n <td>( 14,410.38)</td>\n <td>( 931.18)</td>\n <td>( 16,273.71)</td>\n </tr>\n <tr>\n <th>9</th>\n <td>( 14,492.05)</td>\n <td>( 1,150.51)</td>\n <td>( 1,892.03)</td>\n <td>( 797.60)</td>\n </tr>\n </tbody>\n</table>'
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)))
하지만 이메일은 선행 공백을 무시하는 것과 상관없이 많은 선행 공백이 얼마나 html로 같은 방법으로 표시되지 않습니다. html이 파이썬 출력 (선행 공백 포함)에 나타나는 것과 같은 방법으로 전자 우편에 html 형식의 표시를 만드는 방법을 알아낼 수 없습니다. 더 좋은 방법이 있습니까? 포맷 된 데이터 프레임을 이메일로 보내기보다는 html보다 좋은 포맷이 있습니까?
- 파이썬 3.5.2
- 팬더 0.18.1
- NumPy와 1.11.1
당신은 ...은으로 이메일을 통해 오지 않아 "명확히 할 수 그것은해야합니까 "? 그것은 당신이 입력 한 것과는 다르지만 다른 것입니까? 전혀 아니에요? 이것을 [편집]을 사용하여 질문에 추가 할 수 있습니다. – usr2564301