[英]Suppress the Scientific Notation in Numeric Value Output in Html formatted email written in Python
Hi I have a dataframe which has 3 columns.I want to send this data in the mail after doing html formatting of text and html.I am getting one of the column values in scientific notation(1.53481e+07) in email.I have used round() function to store the value in dataframe as decimal number.I need the the value to show as decimal number(153481.07) instead of scientific notation in my mail output.Kindly help.I am using the below code
import datetime
import psycopg2 as pg
from tabulate import tabulate
import smtplib
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Write Query and Connect to Redshift Database
connection = pg.connect(dbname= '<dbname>', host = '<host>', port= '5439', user= '<user_name>', password='<password>')
cursor = connection.cursor()
cursor.execute("""select 'Exec Dash Revenue' as Metric_Name,SUBSTRING(ptn_dateid_pacific,1,6) as MthYr , sum(grossrev) +sum(shippingrev) + sum(discount) as Metric_value from dw_bo.f_order_component_mart
where hierlevel1 not in ('OTHER', 'Progal', 'Discount', 'Shipping', 'Dummy Discount', 'Gift Card', 'Unassigned') and prodsku_base not in ('UNALLOCATED','TP|UNALLOCATED') and experience in ('WEB', 'APP') and orderstatus in ('W_UPLOAD'
, 'W_PAYMENT_VER'
, 'PAYMENT_VER_REQD'
, 'READY'
, 'CONVTED_TO_JOBS'
, 'INT_PROC'
, 'DONE_INT_PROC'
, 'SENT'
, 'RECEIVED'
, 'PROCESSING'
, 'DONE_PROCESSING'
, 'FULFILLED'
, 'FINAL_PROC'
, 'DONE_FINAL_PROC'
, 'INVOICED'
, 'PAYMENT_REQD'
, 'PAID'
)and ptn_dateid_pacific >= 20200301 and ptn_dateid_pacific <= 20200310 group by 1,2""")
data = pd.DataFrame(cursor.fetchall())
data[2]=data[2].round(2)
data.index = data.index + 1
cursor.close
connection.close()
text = """
Hello, Friends.
Here is your data:
{table}
Regards,
(name)"""
html = """
<html>
<head>
<style>
table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friends</p>
<p>Here is your Report:</p>
{table}
<p>Regards,</p>
<p>(name)</p>
</body></html>
"""
# Set up E-mail
date = datetime.datetime.now()
date = date.strftime('%a %b %d')
fromaddr = '<from_email_address>'
toaddr = ['<to_email_address>']
col_list = ['Metric_Name','MthYr','Metric_value']
text = text.format(table=tabulate(data, headers=col_list, tablefmt="grid"))
html = html.format(table=tabulate(data, headers=col_list, tablefmt="html"))
#html = str(data_html) % data_html
msg = MIMEMultipart("alternative", None, [MIMEText(text), MIMEText(html,'html')])
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Subject'] = 'Recon Report - ' + date
server = smtplib.SMTP('smtp.outlook.com', 587)
server.ehlo()
server.starttls()
server.login(fromaddr, '<password>')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()```
data = pd.DataFrame(cursor.fetchall())
data[2]="%.2f" % (data[2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.