gpt4 book ai didi

python-3.x - python比较单词

转载 作者:行者123 更新时间:2023-12-01 04:04:45 24 4
gpt4 key购买 nike

我遇到了以下问题:
我正在尝试从电子邮件中获取主题并进行比较(它保存在磁盘上,电子邮件为 UTF-8)

import re

def check_subj():
subj = ""
file = open("/home/hikaru/Desktop/sub.eml", "r")

for line in file:
try:
a = re.search("Subject:\ ", line, re.IGNORECASE)
a = line[a.end():]
subj = a
break
except AttributeError:
pass
return subj

print(check_subj())

if check_subj() == 'sub':
print("yay")

对我来说一切都很好,“打印”成功地向我显示“子”,但比较不会为我打印“耶”。
我不明白为什么-(

最佳答案

正如建议的那样,行尾是您问题的可能原因。这是一个更强大的解决方案(仅限循环)

for line in file:
match = re.search("Subject:\ (.*)", line, re.IGNORECASE)
if match:
subj = match.group(1)
break

关于python-3.x - python比较单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com