问题描述
我有一个字符串:
John Smith< jsmith@gmail.com>
我想获得两个变量:
名称(John Smith)和电子邮件(jsmith@gmail.com)
我该怎么做?
感谢您的帮助!
有更多的形式的有效的Internet电子邮件地址比你可能意识到。我建议使用别人的代码来解析它们,例如
例如,以下是一个有效的地址:
Rocky J. Squirrel< rocky.squirrel@gmail.com&
这里的名称是 Rocky J. Squirrel
,而不是Rocky J. Squirrel
。
以下也是合法的语法,并定期显示在邮件中标题(注意缺少<>分隔符):
rocky.squirrel@gmail.com(Rocky J. Squirrel)
尽管括号中的部分技术上只是一个注释,但大多数邮件客户端将其解释为用户名。 (也是Python的email.utils.parseaddr。)
要实际做解析(保存你阅读文档):
>>> import email.utils
pre>
>>> mail.utils.parseaddr(John Smith< jsmith@gmail.com>)
('John Smith','jsmith@gmail.com')
I have a string:
John Smith <jsmith@gmail.com>
I would like to get two variables:
name (John Smith) and email (jsmith@gmail.com)
How might I do that?
Thanks for the help!
解决方案There are more forms of valid Internet Email address than you probably realize. I would suggest using somebody else's code to parse them, like email.utils.parseaddr.
For example, the following is a valid address:
"Rocky J. Squirrel" <rocky.squirrel@gmail.com>
Here, the name is
Rocky J. Squirrel
, not"Rocky J. Squirrel"
.The following is also legal syntax and shows up regularly in mail headers (note lack of <> delimiters):
rocky.squirrel@gmail.com (Rocky J. Squirrel)
Although the part in parens is technically just a "comment", most mail clients interpret it as the user's name. (And so does Python's email.utils.parseaddr.)
To actually do the parsing (saving you reading the docs):
>>> import email.utils >>> email.utils.parseaddr("John Smith <jsmith@gmail.com>") ('John Smith', 'jsmith@gmail.com')
这篇关于解析名字和电子邮件的小字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!