【发布时间】:2013-02-07 11:03:30
【问题描述】:
我正在使用此代码在特定文件中搜索电子邮件并将它们写入另一个文件。我使用了“in”运算符来确保电子邮件不重复。
但是这段代码在for line in f: 行之后不会被执行。
谁能指出我在这里犯的错误?
tempPath = input("Please Enter the Path of the File\n")
temp_file = open(tempPath, "r")
fileContent = temp_file.read()
temp_file.close()
pattern_normal = re.compile("[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+")
pattern_normal_list = pattern_normal.findall(str(fileContent))
with open('emails_file.txt', 'a+') as f:
for item in pattern_normal_list:
for line in f:
if line in item:
print("duplicate")
else:
print("%s" %item)
f.write("%s" %item)
f.write('\n')
【问题讨论】:
-
问题是你第一次运行这个
emails_file.txt会是空的,所以没有行可以读,所以你永远不会有时间添加一个。 @Torxed 正在向您展示解决方案。
标签: python string file python-3.x