循环/特定输出的中的正则表达式



下面的函数有没有办法:

  1. 浏览多个文件
  2. 打印实际的电子邮件(John.doe@gmail.com)在EACH文件中

for循环似乎受到ONE文件中页数的限制。它如何考虑所有15个文件,并打印出电子邮件?

无需列表匹配(如下(:

emails = ["john@example.com", "python-list@python.org", "ug{}ly@email.com"]

我使用以下功能在多个文件中查找电子邮件,但我只从一个文件中得到结果。

for k in range(1,15):
# open the pdf file
object = PyPDF2.PdfFileReader("C:/my_path/file%s.pdf"%(k))
pattern = r""?([-a-zA-Z0-9.`?{}]+@w+.w+)"?" 
NumPages = object.getNumPages()

for i in range(0, NumPages):
PageObj = object.getPage(i)
print("this is page " + str(i)) 
Text = PageObj.extractText() 

for subText in pattern.findall(Text):
print(subText)

我正在寻找的输出:

file1: Jane.Doe@gmail.com
file2: John.doe@yahoo.com
.
.
.
etc

因为您在声明模式变量时脱离了循环。

import re
pattern = re.complile(r""?([-a-zA-Z0-9.`?{}]+@w+.w+)"?")
for k in range(1,15):
# open the pdf file
object = PyPDF2.PdfFileReader("C:/my_path/file%s.pdf"%(k))

for i in range(object.getNumPages()):
PageObj = object.getPage(i)
print("this is page " + str(i)) 
Text = PageObj.extractText() 
for subText in re.findall(pattern, Text):
print(subText)

顺便说一下,我会立即将模式更改为r"?([-a-zA-Z0-9.`?{}]+?@\w+?.\w+?("quot;

最新更新