Skip to main content
 首页 » 编程设计

python之根据字符串类型(样式)转换列表元素

2025年05月04日37小虾米

我有如下列表,

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

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

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

任何努力都会非常值得赞赏。,

提前致谢。

请您参考如下方法:

我认为 '\r'、'\t' 和 '\n' 字符是多余的,应该删除。
虽然我不知道你有什么意图,但我可以提议
将其转换为字典以便将来更容易访问。

m_list = ['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'] 
 
m_dict = {} 
 
for m in m_list: 
    m = m.split(':', maxsplit=1)     
    if len(m) > 1: 
        key, value = m[0], m[1] 
        m_dict[key] = value.strip()  
    else: 
        m_dict['To'] = m_dict['To'] + ' ' + m[0].strip() 
 
print(m_dict) 

输出:
{ 'Message-ID': '<5525962.1075855679785.JavaMail.evans@thyme>', 'Date': 'Wed, 13 Dec 2000 07:04:00 -0800 (PST)', 'From': 'phillip.allen@enron.com', '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', 'Subject': '', 'Mime-Version': '1.0'}