python - 根据字符串类型(样式)转换列表元素
问题描述
我有一个如下列表,
['Message-ID: <5525962.1075855679785.JavaMail.evans@thyme>\r\n',
'Date: Wed, 13 Dec 2000 07:04:00 -0800 (PST)\r\n',
'From: phillip.allen@enron.com\r\n',
'To: christi.nicolay@enron.com, james.steffes@enron.com, jeff.dasovich@enron.com, \r\n',
'\tjoe.hartsoe@enron.com, mary.hain@enron.com, pallen@enron.com,\r\n',
'\tpkaufma@enron.com, richard.sanders@enron.com, \r\n',
'\trichard.shapiro@enron.com, stephanie.miller@enron.com, \r\n',
'\tsteven.kean@enron.com, susan.mara@enron.com, \r\n',
'\trebecca.cantrell@enron.com\r\n',
'Subject: \r\n',
'Mime-Version: 1.0\r\n']
在该To
属性中仅包含 3 个电子邮件 ID,然后是该元素,一些元素以\t
. 实际上那些列表元素是属性\t
的延续。To
我的目标是我想合并To
属性中所有缺失的元素。
到目前为止,我使用下面的代码来解决我的问题。
l=['Message-ID: <5525962.1075855679785.JavaMail.evans@thyme>\r\n',
'Date: Wed, 13 Dec 2000 07:04:00 -0800 (PST)\r\n',
'From: phillip.allen@enron.com\r\n',
'To: christi.nicolay@enron.com, james.steffes@enron.com, jeff.dasovich@enron.com, \r\n',
'\tjoe.hartsoe@enron.com, mary.hain@enron.com, pallen@enron.com,\r\n',
'\tpkaufma@enron.com, richard.sanders@enron.com, \r\n',
'\trichard.shapiro@enron.com, stephanie.miller@enron.com, \r\n',
'\tsteven.kean@enron.com, susan.mara@enron.com, \r\n',
'\trebecca.cantrell@enron.com\r\n',
'Subject: \r\n',
'Mime-Version: 1.0\r\n']
act= [ele.rstrip('\r\n') for ele in l if ele.startswith('To: ')]
rem=[ele.lstrip('\t').rstrip('\r\n') for ele in l if ele.startswith('\t')]
act.extend(rem)
act=[''.join(act)]
l=[ele for ele in l if not ele.startswith('To: ') and not ele.startswith('\t')]
l.extend(act)
print l
输出:
['Message-ID: <5525962.1075855679785.JavaMail.evans@thyme>\r\n',
'Date: Wed, 13 Dec 2000 07:04:00 -0800 (PST)\r\n',
'From: phillip.allen@enron.com\r\n',
'Subject: \r\n',
'Mime-Version: 1.0\r\n',
'To: christi.nicolay@enron.com, james.steffes@enron.com, jeff.dasovich@enron.com, joe.hartsoe@enron.com, mary.hain@enron.com, pallen@enron.com,pkaufma@enron.com, richard.sanders@enron.com, richard.shapiro@enron.com, stephanie.miller@enron.com, steven.kean@enron.com, susan.mara@enron.com, rebecca.cantrell@enron.com']
我认为我的代码变得更加复杂。
是否有任何简单的方法或任何其他更好的方法来解决这个问题?或者我在哪里可以提高我的代码效率?
任何努力都将是非常可观的。
提前致谢。
解决方案
您正在解析电子邮件,这非常棘手,因为有很多极端情况。您应该查看 python电子邮件模块以避免许多陷阱。
import email
headers = ['Message-ID: <5525962.1075855679785.JavaMail.evans@thyme>\r\n',
'Date: Wed, 13 Dec 2000 07:04:00 -0800 (PST)\r\n',
'From: phillip.allen@enron.com\r\n',
'To: christi.nicolay@enron.com, james.steffes@enron.com, jeff.dasovich@enron.com, \r\n',
'\tjoe.hartsoe@enron.com, mary.hain@enron.com, pallen@enron.com,\r\n',
'\tpkaufma@enron.com, richard.sanders@enron.com, \r\n',
'\trichard.shapiro@enron.com, stephanie.miller@enron.com, \r\n',
'\tsteven.kean@enron.com, susan.mara@enron.com, \r\n',
'\trebecca.cantrell@enron.com\r\n',
'Subject: \r\n',
'Mime-Version: 1.0\r\n']
mail = email.message_from_string("".join(headers)+"\r\n"+"foo body") # rebuild mail message and parse
for to in email.utils.getaddresses(mail.get_all("to")):
print(to[1])
生产
christi.nicolay@enron.com
james.steffes@enron.com
jeff.dasovich@enron.com
joe.hartsoe@enron.com
mary.hain@enron.com
pallen@enron.com
pkaufma@enron.com
richard.sanders@enron.com
richard.shapiro@enron.com
stephanie.miller@enron.com
steven.kean@enron.com
susan.mara@enron.com
rebecca.cantrell@enron.com
推荐阅读
- flutter - Flutter 中的 ScrollView 动画
- .net - 正则表达式从完整地址中提取街道,但省略了可选的方向组件
- swift - 视图/应用程序关闭后如何保持计时器运行?
- angular - Angular 8 单元测试,无法将属性“valueAccessor”设置为 null
- python - 无法导入 BeautifulSoup(pylint 导入错误)
- reactjs - react-native-testing-library 工作异步测试,但测试未包含在行为警告中
- javascript - 'typeof Subscription':原型,空
- python - 索引[201] = [0,8] 出现故障。许多稀疏操作需要排序索引。使用 `tf.sparse.reorder` 创建正确排序的副本
- python - 如何将 5(试验 1-5)组值(总共 45 个)附加到 5 个列表
- javascript - mongoose 中的 findById 为某个模式返回 null