重新编译只需要两个论点,有没有办法让它花费更多?或者别的办法?

2024-05-13 22:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在我的电脑上以txt文件的形式访问电子邮件,现在我的目标是从中提取特定的数据。我使用re.compileenumerate通过电子邮件进行解析,寻找匹配的单词(在我的例子中,是像gomcod这样的鱼类),然后打印它们。但是还有100多封邮件我需要解析,每封邮件中都列出了几种不同的鱼类……所以我的问题是:最好的方法是什么?我不能把所有17种不同的鱼类放在一个re.compile函数中,所以我应该有17个不同的代码块,每个代码块中只改变鱼类的种类吗?这是最有效的方法吗?我的代码在下面。你知道吗

import os
import email
import re

path = 'Z:\\folderwithemail'

for filename in os.listdir(path):
file_path = os.path.join(path, filename)
if os.path.isfile(file_path):
    with open(file_path, 'r') as f:
        sector_result = []
        pattern = re.compile("GOM Cod", re.IGNORECASE)
        for linenum, line in enumerate(f):
            if pattern.search(line) != None:
                sector_result.append((linenum, line.rstrip('\n')))
                for linenum, line in sector_result:
                    print ("Fish Species:", line)

Tags: path代码inimportreforos电子邮件
1条回答
网友
1楼 · 发布于 2024-05-13 22:32:52

您可以使用垂直条|在鱼种之间交替

A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B

pattern = re.compile(r"GOM Cod|Salmon|Tuna", re.IGNORECASE)

相关问题 更多 >