gpt4 book ai didi

python - 如何在Python中解析带有括号的电子邮件FROM header ?

转载 作者:行者123 更新时间:2023-12-01 01:20:59 27 4
gpt4 key购买 nike

我在使用 Python 时遇到问题 email用于解析 FROM header 中带有括号的电子邮件的模块。这似乎只是使用 email.policy.default 时的问题。与 email.policy.compat32 相反.

除了切换策略之外,还有其他解决方案吗?

下面是 Python 3.6.5 的最小工作示例:

import email
import email.policy as email_policy

raw_mime_msg=b"from: James Mishra \\(says hi\\) <james@example.com>"

compat32_obj = email.message_from_bytes(
raw_mime_msg, policy=email_policy.compat32)

default_obj = email.message_from_bytes(
raw_mime_msg, policy=email_policy.default)

print(compat32_obj['from'])
print(default_obj['from'])

第一个打印语句返回:
James Mishra \(says hi\) <james@example.com>
第二个打印语句返回:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1908, in get_address
token, value = get_group(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1867, in get_group
"display name but found '{}'".format(value))
email.errors.HeaderParseError: expected ':' at end of group display name but found '\(says hi\) <james@example.com>'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1734, in get_mailbox
token, value = get_name_addr(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1720, in get_name_addr
token, value = get_angle_addr(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1646, in get_angle_addr
"expected angle-addr but found '{}'".format(value))
email.errors.HeaderParseError: expected angle-addr but found '\(says hi\) <james@example.com>'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "test_email.py", line 12, in <module>
print(default_obj['from'])
File "/usr/local/lib/python3.6/email/message.py", line 391, in __getitem__
return self.get(name)
File "/usr/local/lib/python3.6/email/message.py", line 471, in get
return self.policy.header_fetch_parse(k, v)
File "/usr/local/lib/python3.6/email/policy.py", line 162, in header_fetch_parse
return self.header_factory(name, value)
File "/usr/local/lib/python3.6/email/headerregistry.py", line 589, in __call__
return self[name](name, value)
File "/usr/local/lib/python3.6/email/headerregistry.py", line 197, in __new__
cls.parse(value, kwds)
File "/usr/local/lib/python3.6/email/headerregistry.py", line 340, in parse
kwds['parse_tree'] = address_list = cls.value_parser(value)
File "/usr/local/lib/python3.6/email/headerregistry.py", line 331, in value_parser
address_list, value = parser.get_address_list(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1931, in get_address_list
token, value = get_address(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1911, in get_address
token, value = get_mailbox(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1737, in get_mailbox
token, value = get_addr_spec(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1583, in get_addr_spec
token, value = get_local_part(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1413, in get_local_part
obs_local_part, value = get_obs_local_part(str(local_part) + value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1454, in get_obs_local_part
token, value = get_word(value)
File "/usr/local/lib/python3.6/email/_header_value_parser.py", line 1340, in get_word
if value[0]=='"':
IndexError: string index out of range

最佳答案

email.policy.default 旨在符合电子邮件 RFC,但您的邮件不符合 RFC 5322 。如果括号内的部分应该是注释,那么消息应该如下所示

raw_mime_msg=b"from: James Mishra (says hi) <james@example.com>"

要合规。如果它不应该是注释,那么括号应该出现在带引号的字符串内。这可能看起来像

raw_mime_msg=b'from: "James Mishra (says hi)" <james@example.com>'
<小时/>

由于您的消息不合规,因此使用期望合规的策略并不合适。如果您想处理不合规的邮件,email.policy.compat32 是比 email.policy.default 更好的选择。

关于python - 如何在Python中解析带有括号的电子邮件FROM header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841855/

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