将多个for循环输出附加到列表中

问题描述 投票:0回答:2

我正在使用RegEx从txt文件中提取一些数据。我在下面的for循环中提取了电子邮件和生日,并尝试将输出附加到列表中。但是,当我打印列表时,仅打印第一个附加的输出。 birtdate RegEx单独运行时可以正常工作。我确定我在做一些非常基本的错误。

f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")

list = []

for i in f:
    if re.findall(r"((?i)[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.])", i):
        list.append(i)

for k in f:
    if re.findall(r'\d\d-\d\d-\d\d\d\d', k):
        list.append(k)

print(list)
f.close()
python for-loop append
2个回答
1
投票

尝试一下:

with open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8") as f:
    i = f.readline()
    if re.findall(r"((?i)[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.])", i):
        list.append(i)
    if re.findall(r'\d\d-\d\d-\d\d\d\d', k):
        list.append(i)

在您的代码中,在第一个for循环之后,f现在指向文件的末尾,因此第二个for循环不会像您打算运行的那样“运行”。

因此,要修改代码以使其达到预期的效果,您将在第一个循环之后关闭文件,然后在第二个循环之前重新打开文件,以使文件指针f再次指向文件的开头:

f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")

list = []

for i in f:
    if re.findall(r"((?i)[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.])", i):
        list.append(i)

f.close()

f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")
for k in f:
    if re.findall(r'\d\d-\d\d-\d\d\d\d', k):
        list.append(k)

print(list)
f.close()

1
投票

您尝试两次读取相同的文件。第二个for循环不会执行任何操作。看看这个了解:

f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")
print(list(f))
print("second time:")
print(list(f))

输出:

['1234567890abcdefghijklmopqrstuvwxyz'] # or whatever your content is :)
second time:
[]

要解决此问题,您可以将文件结果存储在列表中:

f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")
content = list(f)


for i in content:
   ... 

for k in content:
   ... 
© www.soinside.com 2019 - 2024. All rights reserved.