当re.com.plile找不到某种模式时,我可以向自己发送电子邮件吗?

发布于 2025-02-04 08:45:45 字数 2011 浏览 4 评论 0原文

目前,我的代码会浏览我的电子邮件,并寻找某种模式。我想做到这一点,以便如果一封电子邮件没有该模式,它将将该电子邮件发送到某个电子邮件。任何帮助都将受到赞赏:)

with open("data.csv", "w") as f_out:
writer = csv.writer(f_out)

for i in range(messages, messages-N, -1):
    # fetch the email message by ID
    res, msg = imap.fetch(str(i), "(RFC822)")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject, encoding = decode_header(msg["Subject"])[0]
            if isinstance(subject, bytes):
                # if it's a bytes, decode to str
                subject = subject.decode(encoding)
            # decode email sender
            From, encoding = decode_header(msg.get("From"))[0]
            if isinstance(From, bytes):
                From = From.decode(encoding)
            print("Subject:", subject)
            print("From:", From)

                # iterate over email parts
            for part in msg.walk():
                # extract content type of email
                content_type = part.get_content_type()
                content_disposition = str(part.get("Content-Disposition"))
                payload = part.get_payload(decode=True)
                if payload is None:
                    continue
                body = payload.decode()                   
                pattern = re.compile(
                    r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+)( Line ([0-9]+))?( Seq ([0-9])? ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+)( Line ([0-9]+ )| )(Seq ([0-9]) |)([0-9]+\/[0-9]+\/[0-9]+)")

                matches = pattern.finditer(body)
                writer.writerows(map(lambda m: m.groups(), matches))
                break

imap.close() imap.logout()

Currently, my code goes through my emails and looks for a certain pattern. I wanted to make it so that if an email does not have that pattern, it'll send that email to a certain email. Any help is appreciated :)

with open("data.csv", "w") as f_out:
writer = csv.writer(f_out)

for i in range(messages, messages-N, -1):
    # fetch the email message by ID
    res, msg = imap.fetch(str(i), "(RFC822)")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject, encoding = decode_header(msg["Subject"])[0]
            if isinstance(subject, bytes):
                # if it's a bytes, decode to str
                subject = subject.decode(encoding)
            # decode email sender
            From, encoding = decode_header(msg.get("From"))[0]
            if isinstance(From, bytes):
                From = From.decode(encoding)
            print("Subject:", subject)
            print("From:", From)

                # iterate over email parts
            for part in msg.walk():
                # extract content type of email
                content_type = part.get_content_type()
                content_disposition = str(part.get("Content-Disposition"))
                payload = part.get_payload(decode=True)
                if payload is None:
                    continue
                body = payload.decode()                   
                pattern = re.compile(
                    r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+)( Line ([0-9]+))?( Seq ([0-9])? ([0-9]+/[0-9]+/[0-9]+)")
                    #r"([a-zA-Z]+[0-9]+)( Line ([0-9]+ )| )(Seq ([0-9]) |)([0-9]+\/[0-9]+\/[0-9]+)")

                matches = pattern.finditer(body)
                writer.writerows(map(lambda m: m.groups(), matches))
                break

imap.close()
imap.logout()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2025-02-11 08:45:45

您有一个Python的正则表达式

pattern = re.compile(r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")

和一些字符串

body

模式。如果没有匹配项,则返回空列表。模式。匹配(身体)如果没有匹配项,则不会返回。因此

if not pattern.findall(body):
    ...  # send email

if pattern.match(body) is None:
    ...  # send email

适合您的reqs。 tatter.finditer(身体)总是返回迭代器,这是一个真实的对象; 该声明的主体

if not pattern.finditer(body):

从未执行。

You have a python regex

pattern = re.compile(r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)")

and some string

body

pattern.findall(body) returns the empty list if there are no matches. pattern.match(body) returns None if there are no matches. So

if not pattern.findall(body):
    ...  # send email

or

if pattern.match(body) is None:
    ...  # send email

fit your reqs. pattern.finditer(body) always returns an iterator, which is a truthy object; the body of the

if not pattern.finditer(body):

statement is never executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文