代码之家  ›  专栏  ›  技术社区  ›  Mohamed Thasin ah

基于字符串类型(样式)转换列表元素

  •  1
  • Mohamed Thasin ah  · 技术社区  · 6 年前

    我有下面的清单,

    ['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 \t . 实际上那些 \t型 属性。

    到目前为止,我使用下面的代码来解决我的问题。

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

    我想我的代码变得更复杂了。

    或者在哪里可以提高代码效率?

    提前谢谢。

    3 回复  |  直到 6 年前
        1
  •  2
  •   hamster ham    6 年前


    虽然我不知道你有什么打算,我可以向你求婚吗
    将其转换为字典,以便将来更容易访问。

    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'}

        2
  •  2
  •   Setop    6 年前

    你正在分析电子邮件,这是相当棘手的,因为有许多角落的情况。你应该看看蟒蛇 email module 为了避免很多陷阱。

    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
    
        3
  •  1
  •   Abhisek Roy    6 年前

      arr=['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']
    
    reqd_array=[]
    k=""
    
    for i in arr:
        if ':' in i:
            reqd_array.append(i.strip())
        else:
            k=k+i.strip()
    for i,j in enumerate(reqd_array):
        if j.startswith("To:"):
            reqd_array[i]=reqd_array[i]+k
            break
    print(reqd_array)