[英]python: reading an inline label txt file and formating it to columns
我想对我的电子邮件进行统计分析。 为此,我选择带有Outlook的有趣电子邮件,然后将其保存为txt文件。
以下是您可以找到的样本(或近似由于翻译而来):
Send: monday 9 jully 2018 12:00 To: john doe Cc: sister doe; brother doe; mother doe Object: my data issue enclosed: data.pdf Send: monday 9 jully 2018 12:00 To: john doe Cc: sister doe; brother doe; mother doe Object: my data issue enclosed: data.pdf Send: monday 9 jully 2018 12:00 To: john doe Cc: sister doe; brother doe; mother doe Object: my data issue enclosed: data.pdf
显然,要管理我的数据,最好将其分成几列。 每封电子邮件的列标签分别为{发送,收件人,抄送,对象,封闭}和一行。
我敢肯定,存在一种很好的方法来做到这一点,也许是对熊猫来说,但是我没有使用好的关键字来找到有效的答案。
有什么小窍门帮助我吗?
假设:
1)每个电子邮件信息集之间都有一个空行
2)在每个信息集中,您总是有5列(发送,到,抄送,对象,封闭),并且它们始终以相同的顺序出现
3)没有空数据(例如-所有电子邮件都有附件等)
input="""Send: monday 9 jully 2018 12:00
To: john doe
Cc: sister doe; brother doe; mother doe
Object: my data issue
enclosed: data.pdf
Send: monday 9 jully 2018 12:00
To: john doe
Cc: sister doe; brother doe; mother doe
Object: my data issue
enclosed: data.pdf
Send: monday 9 jully 2018 12:00
To: john doe
Cc: sister doe; brother doe; mother doe
Object: my data issue
enclosed: data.pdf"""
emails = input.split('\n\n')
output = list()
for email in emails:
lines = email.split('\n')
row=list()
for line in lines:
row.append(line.split(':')[1].strip())
output.append(row)
print(output)
output
将是列表的列表-在您的示例中为3行乘5列。 以后可以根据需要相对轻松地将其转换为数据帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.