首页 > 解决方案 > 根据字符串类型(样式)转换列表元素

问题描述

我有一个如下列表,

['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']

我认为我的代码变得更加复杂。

是否有任何简单的方法或任何其他更好的方法来解决这个问题?或者我在哪里可以提高我的代码效率?

任何努力都将是非常可观的。

提前致谢。

标签: pythonlistlist-comprehension

解决方案


您正在解析电子邮件,这非常棘手,因为有很多极端情况。您应该查看 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

推荐阅读