Python变量赋值不存在

2021-09-27 06:39:04 发布

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

>>> header = 'subject'
>>> msg = email.message_from_string(raw_email)   
>>> msg[header] = 'Test123'
>>> print msg[header]
=?UTF-8?B?UmU6IFtHQlMgU3lzdGVtYWRtaW5pc3RyYXRpb25dICMxMjogRS1NYWk=?=
 =?UTF-8?B?bCBCZW5hY2hyaWNodHVuZyBmw7xyIG5ldWUgVGlja2V0cyBlaW5yaWNodGVu?=

为什么msg[header]不包含'Test123'?你知道吗

2条回答
网友
1楼 ·

根据文件:

__setitem__(name, val)

Add a header to the message with field name name and value val. The field is appended to the end of the message’s existing fields.

Note that this does not overwrite or delete any existing header with the same name. If you want to ensure that the new header is the only one present in the message with field name name, delete the field first, e.g.:

del msg['subject']
msg['subject'] = 'Python roolz!'

http://docs.python.org/2/library/email.message.html#email.message.Message

网友
2楼 ·

http://docs.python.org/2/library/email.message.html#email.message.Message.setitem

请注意,这不会覆盖或删除任何同名的现有标头。如果要确保新的标头是消息中仅有的字段名为name的标头,请先删除字段“

相关问题