我有一个包含一些电子邮件的文本文件。所有这些都开始如下:
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
From: gsilver@umich.edu
From: gsilver@umich.edu
From: zqian@umich.edu
From: gsilver@umich.edu
From: wagnermr@iupui.edu
From: zqian@umich.edu
From: antranig@caret.cam.ac.uk
From: gopal.ramasammycook@gmail.com
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: louis@media.berkeley.edu
From: ray@media.berkeley.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
我的问题是要获得唯一的电子邮件 ID,所以我尝试-
fhand = open("mbox-short.txt")
emails=[]
for line in fhand:
if line.startswith("From:"):
l=line.lstrip("From:").rstrip()
emails.append(l)
unique = []
for email in emails:
if email not in unique:
unique.append(email)
print(email)
print("\nTotal Unique Contacts=",len(unique))
输出-
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
wagnermr@iupui.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
ray@media.berkeley.edu
唯一联系人总数= 11
这是正确的答案但是-
当我在 lstrip("From: ") 中使用了一个额外的空间时,因为实际的电子邮件在 "From:" 之后开始,这就是我得到的-
fhand = open("mbox-short.txt")
emails=[]
for line in fhand:
if line.startswith("From:"):
l=line.lstrip("From: ").rstrip()
emails.append(l)
unique = []
for email in emails:
if email not in unique:
unique.append(email)
print(email)
print("\nTotal Unique Contacts=",len(unique))
输出-
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
jlowe@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
wagnermr@iupui.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
ay@media.berkeley.edu
唯一联系人总数= 11
所以我们看到从 r 开始的电子邮件受到影响,因为它们的第一个字母从输出中消失,而其他的则根本不受影响。请帮助我理解为什么会这样。谢谢
阿晨1998
交互式爱情
qq_遁去的一_1
相关分类