解析字典值Python

时间:2016-12-13 17:51:03

标签: python dictionary

我如何解析下面的dictionary,以便values只包含票号?

当前词典:

{'8.8.8.8': 'Open Menu  10A-003272 10A-003328 10A-003652', '8.8.8.9': '10A-003069 10/21/2016', '8.8.8.10': 'Open Menu  10A-003145 10/21/2016'}

目标词典:

{'8.8.8.8': '10A-003272 10A-003328 10A-003652', '8.8.8.9': '10A-003069', '8.8.8.10': '10A-003145'}

用于制作字典的代码:

with open(esccbList, 'r') as f:
    d = {}
    for line in f:
        d[line.strip()] = next(f, '').strip()

正则表达式找到票号:

n = re.search(r'10A-\d{6}',item, re.M|re.I)

4 个答案:

答案 0 :(得分:3)

我更新了我的答案,以所需的格式打印字典。

import re

pattern = re.compile(r'10A-\d{6}')
info = {'8.8.8.8': 'Open Menu  10A-003272 10A-003328 10A-003652', 
        '8.8.8.9': '10A-003069 10/21/2016', 
        '8.8.8.10': 'Open Menu  10A-003145 10/21/2016'}

output = {}
for key, value in info.items():
    tokens = value.split()
    val = ''
    for token in tokens:
        if pattern.match(token):
            val = val + token + ' '
    val = val.strip()
    output[key] = val;

print(output)

打印:

{'8.8.8.8': '10A-003272 10A-003328 10A-003652', 
 '8.8.8.9': '10A-003069', 
 '8.8.8.10': '10A-003145'}

答案 1 :(得分:2)

假设您的票号子字符串仅包含连字符-,您可以使用 dict comprhension 来实现此目的:

my_dict = {'8.8.8.8': 'Open Menu  10A-003272 10A-003328 10A-003652', '8.8.8.9': '10A-003069 10/21/2016', '8.8.8.10': 'Open Menu  10A-003145 10/21/2016'}

new = {k: ' '.join(i for i in v.split() if '-' in i) for k, v in my_dict.items()}

new dict持有的最终价值将是:

{'8.8.8.9': '10A-003069', 
 '8.8.8.10': '10A-003145', 
 '8.8.8.8': '10A-003272 10A-003328 10A-003652'}

答案 2 :(得分:0)

d = { k, clean_ticket(v) for k,v in original_dict.items() if is_ticket(v) }

看起来像is_ticket应该像

def is_ticket(v):
    return "Open Menu" in v

创建一个删除打开菜单的函数clean_ticket(v)

def clean_ticket(v):
    return v.split("Open Menu")[1].strip()

类似的东西。

答案 3 :(得分:-2)

我假设你有一些功能

def is_ticket_number(item):
    """ returns True only if item is a ticket number """
    return re.search(r'10A-\d{6}',item, re.M|re.I)

然后您需要做的就是

d = {k: v for k, v in d.items() if is_ticket_number(v)}