yagmail 添加额外的换行符在pandas DataFrame 生成的基因组中

2024-06-01 23:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用本文中详细介绍的方法发送一封包含pandas数据帧的yagmail电子邮件:Python Email in HTML format mimelib 。在

但是,不管我做什么尝试,在打印数据帧之前,yagmail都会添加一长串换行符。在

我的代码是(我只更改了安装的电子邮件地址):

#--- Addition to original post ---
import pandas as pd
df = pd.DataFrame([[900, 20.0], [500, 21.0]], columns =['Quantity', 'Price'])
#--- Addition to original post ---

import time
import yagmail
yag = yagmail.SMTP(username, password)

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df.to_html()

yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [text,html])

生成的电子邮件正文是:

^{pr2}$

我还尝试添加DOCTYPE标记,编码为utf-8,从unicode转换为string,但没有成功。在

这是一个错误,还是有人能帮我解决问题?在

编辑1: 请注意,html变量包含以下内容:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Quantity</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>900</td>
      <td>20.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>500</td>
      <td>21.0</td>
    </tr>
  </tbody>
</table>

编辑2: 为了详细说明我尝试添加html和doctype标记,我尝试在html的开头和结尾添加以下内容。在

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>Title</title>
</head>
<body>
...
</body>
</html>

我验证了这是有效的htmlhere,并且生成的电子邮件正文是相似的。在

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

























    Quantity    Price
0   900     20.0
1   500     21.0

Tags: totextorgimport电子邮件htmlwwwprice
1条回答
网友
1楼 · 发布于 2024-06-01 23:18:07

评论时间过长:可以做的一件事是在发送前创建输出变量,并手动删除空格。例如,在我过去构建的一个电子邮件程序中,我使用easygui并使用textbox为用户创建邮件。那就发个短信。一个小例子:

import easygui as e
text_mes= e.textbox("Add to the Email", "Create Your Email", html)
yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [text,text_mes])

这将允许您手动删除行,直到您能够找到方法创建一些代码来消除空格。在

相关问题 更多 >