我有一个小脚本:
#!/usr/bin/python3.2
#takes the bad_emails out of full_emails and leaves us with good_emails
#This is a manually generated list of bad emails (bounce backs)
bad_list = []
with open("bad_emails.txt","r") as bad:
for line in bad.readlines():
bad_list.append(line)
#this is a list of ALL email addresses output by AcyMailing
full_emails = []
with open("full_emails.txt","r") as full:
for email in full.readlines():
if email in bad_list:
pass
else:
full_emails.append(email)
#this is a final list containing only the email addresses with want
good_list = []
with open("good_emails","w") as good:
for email in full_emails:
good.write(email)
我正在尝试做的是简短的:从Joomla中名为AcyMailing的邮件程序中获取电子邮件地址列表并将其导出。它具有以下格式: “abc@abc.com” “def@def.com” “etc@etc.etc”
虽然我的上述脚本有效(它消除了'糟糕的电子邮件'并且只留下'好的电子邮件'我还没有找到一种方法让每个电子邮件都被引号括起来,如AcyMailing(Joomla)使用我已经看到很多人使用正则表达式执行这样的任务。这是在python中执行此操作的唯一方法吗?
答案 0 :(得分:0)
您应该能够将字符串包装在引号中:
good.write('"' + email.strip() + '"\n')
或者您可以使用.format
:
good.write('"{}"\n'.format(email.strip()))
答案 1 :(得分:0)
.readlines()
的for循环是多余的。您可以使用set.difference()
查找好的电子邮件:
# read all emails from the file, one email per line, skip blank lines
read_emails = lambda file: (line.strip() for line in file if line.strip())
with open('bad_emails.txt') as bad, open('full_emails.txt') as full:
good_emails = set(read_emails(full)).difference(read_emails(bad))
with open('good_emails.txt', 'w') as good:
good.writelines('"%s"\n' % email for email in good_emails)