根据条件从列表中创建字典键/值对

时间:2018-06-18 17:05:18

标签: python list dictionary imaplib

请原谅我,如果这是一团糟,但我还是有点新意。 我需要从电子邮件中的数据创建新变量。 我使用imaplib在我们的服务器上阅读工作票据的电子邮件。我已经能够从电子邮件中提取我需要的数据,但现在我必须将其组织成可用的东西。这是我用来获取数据的代码;

dataList = []

def parser(data):
    # this will parse the data from ticket and create a list.
    html = data
    parsed = bs(html, "lxml")
    data = [line.strip() for line in parsed.stripped_strings]
    dataList.append(data)


imapUser = "domain\\ticketemailaccount"
imapPassw = "passwordthatisused"
conn = imaplib.IMAP4_SSL('mail.company.com', 993)
try:
    conn.login(imapUser, imapPassw)
except:
    print "##########  SERVER LOGIN ERROR  ##########"
    print sys.exc_info()[1]
    sys.exit(1)

conn.select(readonly=1)
result, data = conn.search(None, '(UNSEEN)')
conn.select("inbox")
ids = data[0].split() #each email has a sequential ID.

for d in ids:
    result, data = conn.fetch(d, "(UID BODY.PEEK[TEXT])")
    raw_email = data[0][1]
    msg = email.message_from_string(raw_email)
    tic = str(msg)
    parser(tic)

for i in dataList:
    ticketNum = i[1] #this is a unique ID for each ticket
    print ticketNum
    print i

这个的输出看起来像这样;

181693185
[u'From nobody Mon Jun 18 10:07:54 2018', u'121314151', u'WORK TICKET REQUEST', u'TICKET NUMBER:', u'181694524', u'OLD TICKET NUM:', u'Message Type:', u'Normal', u'For Code:', u'TRUCK1', u'Hours Notice:', u'72', u'Seq Num:', u'10', u'Prepared By:', u'Bob.1234', u'Call Back:', u'Work Information', u'State:', u'ZA', u'Work To Begin:', u'06/21/18 AT 10:00', u'County:', u'SOMECOUNTY', u'Update Date:', u'07/02/18 AT 00:00', u'Place:', u'GOTHAM CITY', u'Extent:', u"Add'l Addr In Remarks:", u'No'] 

我的问题是,如果遇到的第一个项目以“:”结尾,我如何查看列表并创建两个项目的键/值对,使列表中的以下项目仅为值不以“:”结尾?

2 个答案:

答案 0 :(得分:1)

使用forzip的一种方式:

L = [u'From nobody Mon Jun 18 10:07:54 2018', u'121314151', u'WORK TICKET REQUEST', u'TICKET NUMBER:', u'181694524', u'OLD TICKET NUM:', u'Message Type:', u'Normal', u'For Code:', u'TRUCK1', u'Hours Notice:', u'72', u'Seq Num:', u'10', u'Prepared By:', u'Bob.1234', u'Call Back:', u'Work Information', u'State:', u'ZA', u'Work To Begin:', u'06/21/18 AT 10:00', u'County:', u'SOMECOUNTY', u'Update Date:', u'07/02/18 AT 00:00', u'Place:', u'GOTHAM CITY', u'Extent:', u"Add'l Addr In Remarks:", u'No']

d = {}

for i, j in zip(L, L[1:]):
    if i.endswith(':') and not j.endswith(':'):
        d[i] = j

你也可以把它写成字典理解:

d = {i: j for i, j in zip(L, L[1:]) if i.endswith(':') and not j.endswith(':')}

结果:

print(d)

{"Add'l Addr In Remarks:": 'No',
 'Call Back:': 'Work Information',
 'County:': 'SOMECOUNTY',
 'For Code:': 'TRUCK1',
 'Hours Notice:': '72',
 'Message Type:': 'Normal',
 'Place:': 'GOTHAM CITY',
 'Prepared By:': 'Bob.1234',
 'Seq Num:': '10',
 'State:': 'ZA',
 'TICKET NUMBER:': '181694524',
 'Update Date:': '07/02/18 AT 00:00',
 'Work To Begin:': '06/21/18 AT 10:00'}

答案 1 :(得分:-1)

您只需使用zip即可执行以下操作:

[18/Jun/2018 17:46:45] "POST /app/ HTTP/1.1" 302 0
[18/Jun/2018 17:46:45] "GET /app/loaded_data/ HTTP/1.1" 200 26

输出:

l = [u'From nobody Mon Jun 18 10:07:54 2018', u'121314151', u'WORK TICKET REQUEST', u'TICKET NUMBER:', u'181694524', u'OLD TICKET NUM:', u'Message Type:', u'Normal', u'For Code:', u'TRUCK1', u'Hours Notice:', u'72', u'Seq Num:', u'10', u'Prepared By:', u'Bob.1234', u'Call Back:', u'Work Information', u'State:', u'ZA', u'Work To Begin:', u'06/21/18 AT 10:00', u'County:', u'SOMECOUNTY', u'Update Date:', u'07/02/18 AT 00:00', u'Place:', u'GOTHAM CITY', u'Extent:', u"Add'l Addr In Remarks:", u'No']

b = dict(zip(l[::2], l[1::2]))