【发布时间】:2018-01-08 16:03:13
【问题描述】:
我想用 Python 从几个文本文件中解析出电子邮件地址。在第一次尝试中,我尝试从字符串列表 ('2To whom correspondence should be addressed. E-mail: joachim+pnas@uci.edu.\n') 中获取包含电子邮件地址的以下元素。
当我尝试通过i.find("@") == 0 查找包含电子邮件地址的列表元素时,它没有给我content[i]。我误解了.find() 功能吗?有没有更好的方法来做到这一点?
from os import listdir
TextFileList = []
PathInput = "C:/Users/p282705/Desktop/PythonProjects/ExtractingEmailList/text/"
# Count the number of different files you have!
for filename in listdir(PathInput):
if filename.endswith(".txt"): # In case you accidentally put other files in directory
TextFileList.append(filename)
for i in TextFileList:
file = open(PathInput + i, 'r')
content = file.readlines()
file.close()
for i in content:
if i.find("@") == 0:
print(i)
【问题讨论】:
-
find 函数返回找到的字符串的索引。如果您只是想查看字符串中是否包含“@”,那么 i.find("@") != -1,因为 -1 表示字符串中没有“@”
标签: python string python-3.x parsing