[英]Can not read email content from gmail after extracting with python program
[英]Extracting table from email using gmail api in python
我想从 email 中提取表格,在 email 客户端查看邮件时显示表格
这是 email 快照
我想处理表,但找不到在 python 代码中获取它的方法
这是原始数据的摘录
decoded_data = base64.b64decode(data)
正在显示b'a dg\r\nb eh\r\nc fj\r\na d\r\nb eh\r\nc fj\r\n\r\nBest Regards,\r\nVikrant Pawar\r\n'
虽然汤给它喜欢
soup = BeautifulSoup(decoded_data, "lxml")
表明
<html><body><p>a d g
b e h
c f j
a d
b e h
c f j
Best Regards,
Vikrant Pawar
</p></body></html>
有没有办法让我可以在 pandas 中导入表格数据
您可以从中拆分数据并形成表格列表:
from bs4 import BeautifulSoup
import pandas as pd
text = """
<html><body><p>a d g
b e h
c f j
a d
b e h
c f j
Best Regards,
Vikrant Pawar
</p></body></html>
"""
soup = BeautifulSoup(text, 'lxml')
data = soup.p.text
list_of_tables = data.split('\n')
# -> ['a d g', 'b e h', 'c f j', 'a d', 'b e h', 'c f j', '', 'Best Regards,', 'Vikrant Pawar', '']
请注意,如果有额外的\r
和\n
,则应按data.split('\n\r')
拆分。 现在您可以获得形成 pandas df 所需的零件。 假设您只想要“Best Regards”之前的部分。 为此,我们首先需要对列表进行切片,然后拆分每个元素以形成 pandas df:
list_of_tables = [each.split() for each in list_of_tables[:6]]
# -> [['a', 'd', 'g'], ['b', 'e', 'h'], ['c', 'f', 'j'], ['a', 'd'], ['b', 'e', 'h'], ['c', 'f', 'j']]
现在我们需要做的就是形成 dataframe:
df = pd.DataFrame(list_of_tables)
最终结果如下所示:
0 1 2
0 a d g
1 b e h
2 c f j
3 a d None
4 b e h
5 c f j
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.