当re.com.plile找不到某种模式时,我可以向自己发送电子邮件吗?
目前,我的代码会浏览我的电子邮件,并寻找某种模式。我想做到这一点,以便如果一封电子邮件没有该模式,它将将该电子邮件发送到某个电子邮件。任何帮助都将受到赞赏:)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个Python的正则表达式
和一些字符串
模式。如果没有匹配项,则返回空列表。模式。匹配(身体)如果没有匹配项,则不会返回。因此
或
适合您的reqs。 tatter.finditer(身体)总是返回迭代器,这是一个真实的对象; 该声明的主体
从未执行。
You have a python regex
and some string
pattern.findall(body) returns the empty list if there are no matches. pattern.match(body) returns None if there are no matches. So
or
fit your reqs. pattern.finditer(body) always returns an iterator, which is a truthy object; the body of the
statement is never executed.