Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3.2中,将输出的电子邮件地址用引号括起来_Python - Fatal编程技术网

在Python3.2中,将输出的电子邮件地址用引号括起来

在Python3.2中,将输出的电子邮件地址用引号括起来,python,Python,我有一个小剧本: #!/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

我有一个小剧本:

#!/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)使用。我已经看到很多人使用regex来完成这样的任务。这是python中实现这一点的唯一方法吗?

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

good.write('"' + email.strip() + '"\n')
或者您可以使用
.format

good.write('"{}"\n'.format(email.strip()))

带有
.readlines()
的for循环是多余的。您可以使用以下方法查找好的电子邮件:

# 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)

结果是“aaa@aaa.com(然后换行)“”bbb@bbb.com(重复)我假设,因为正在编写的元素中已经有新行。在阅读第一个文件时,您从未从电子邮件中删除该行。让我将其编辑到答案中。现在我明白了,感谢Blender花时间回答此问题。