【发布时间】:2021-05-13 22:48:35
【问题描述】:
我有以下用于电子邮件活动的 act2.txt 文件:
2021-04-02//email@example.com//Enhance your presentation skills in 15 minutes//Open
2021-04-11//email@example.com//Enroll in the presentations skills - FREE WEBINAR//Open
2021-04-11//email@example.com//Enroll in the presentations skills - FREE WEBINAR//Delivered
2021-04-11//email@example.com//Enroll in the presentations skills - FREE WEBINAR//Delivered
2021-04-11//email@example.com//Enroll in the presentations skills - FREE WEBINAR//Delivered
2021-04-16//email@example.com//YOU ARE INVITED TO THIS PROGRAMMING EVENT//Delivered
2021-04-01//email@example.com//Enhance your presentation skills in 15 minutes//Delivered
2021-04-09//email@example.com//we are here to help you improve your skills//Delivered
2021-04-12//email@example.com//(1st meeting) here is our recorded presentation skills webinar//Delivered
2021-04-13//email@example.com//YOU ARE INVITED TO THIS PROGRAMMING EVENT//Delivered
我想按客户跟踪电子邮件活动 - 我计算了已发送的电子邮件、已发送的电子邮件然后打开率。
我生成了两个列表,一个用于发送的电子邮件,另一个用于打开的电子邮件:
import re
from pprint import pprint
#read the file with activities separated by //
afile = "act2.txt"
afile_read = open(afile,"r")
lines = afile_read.readlines()
activityList = []
for activities in lines:
activity = activities.split("//")
date = activity[0]
customer_email = activity[1]
email_title = activity[2]
action = activity[3]
stripped_line = [s.rstrip() for s in activity]
activityList.append(stripped_line)
#print (activityList)
stripped_email = 'email@example.com'
email_actions = [x for x in activityList if stripped_email in x[1]]
delivered = [x for x in email_actions if 'Delivered' in x]
Opened = [x for x in email_actions if 'Open' in x]
delcount = (len(delivered))
opencount = (len(Opened))
try:
Open_rate = opencount / delcount * 100
except ZeroDivisionError:
Open_rate = 0
print (stripped_email,",", delcount,",", opencount,",", Open_rate,"%")
pprint(delivered)
pprint (Opened)
交付清单:
[['2021-04-11',
'email@example.com',
'Enroll in the presentations skills - FREE WEBINAR',
'Delivered'],
['2021-04-11',
'email@example.com',
'Enroll in the presentations skills - FREE WEBINAR',
'Delivered'],
['2021-04-11',
'email@example.com',
'Enroll in the presentations skills - FREE WEBINAR',
'Delivered'],
['2021-04-16',
'email@example.com',
'YOU ARE INVITED TO THIS PROGRAMMING EVENT',
'Delivered'],
['2021-04-01',
'email@example.com',
'Enhance your presentation skills in 15 minutes',
'Delivered'],
['2021-04-09',
'email@example.com',
'we are here to help you improve your skills',
'Delivered'],
['2021-04-12',
'email@example.com',
'(1st meeting) here is our recorded presentation skills webinar',
'Delivered'],
['2021-04-13',
'email@example.com',
'YOU ARE INVITED TO THIS PROGRAMMING EVENT',
'Delivered']]
打开列表:
[['2021-04-02',
'email@example.com',
'Enhance your presentation skills in 15 minutes',
'Open'],
['2021-04-11',
'email@example.com',
'Enroll in the presentations skills - FREE WEBINAR',
'Open']]
我想比较两个列表并生成第三个列表(组合活动),按电子邮件主题过滤 - 如果主题在已发送列表和打开列表中,那么它将被计为一个活动。但是,邮件主题可以重复,就像邮件发送了 3 次但只打开了一次。因为我还在学习 python,所以我找不到合适的逻辑。
为更清楚而编辑:
如果在按标题过滤的打开列表中找到一封电子邮件,则应在最后日期之前从已发送列表中删除相同的标题,并生成包含组合活动的新列表。
【问题讨论】:
-
既然您无论如何都希望对它们进行排序,我将向您指出to
itertools.groupby; read the docs,这基本上是对已排序输入进行重复数据删除的规范方法。heapq.mergemay be useful as well,如果您正在分别处理/排序打开和交付的输入,那么希望将排序的结果合并在一起。 -
我不太明白你想要从这两个列表中得到什么输出。
-
嗨@BoobyTrap 基本上,如果在打开的列表中找到具有相同标题的电子邮件 - 那么它将从交付的列表项中删除并为所有(交付和打开)生成一个组合列表。
-
你要的输出是delivered和opened的组合?如果是这样,为简单起见,您可以使用带有键为标题的字典。但我怀疑所有打开的项目都在交付列表中
标签: python python-3.x loops nested-lists