1

我有一个小脚本:

#!/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 中执行此操作的唯一方法吗?

4

2 回答 2

0

您应该能够将字符串用引号括起来:

good.write('"' + email.strip() + '"\n')

或者您可以使用.format

good.write('"{}"\n'.format(email.strip()))
于 2012-11-03T08:13:32.150 回答
0

带有的 for 循环.readlines()是多余的。您可以使用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)
于 2012-11-03T08:53:32.490 回答