Python:将字符串列表解析为字典

时间:2014-10-17 06:09:49

标签: python parsing

这有点复杂。我有一个如下所示的列表:

['19841018 ID1\n', ' Plunging oil... \n', 'cut in the price \n', '\n', '19841018 ID2\n', ' The U.S. dollar...  \n', 'the foreign-exchange markets \n', 'late New York trading \n', '\n']

在我的列表中,'\n'是一个独立的故事。我想做的是从上面的列表中创建一个字典:

dict = {ID1: [19841018, 'Plunging oil... cut in the price'], ID2: [19841018, 'The U.S. dollar... the foreign-exchange markets']}

您可以看到我的KEY我的词典是ID,而项目是year以及故事的组合。那是可行的吗?

  • 我的ID采用此格式J00100394J00384932。所以他们都以J00开头。

2 个答案:

答案 0 :(得分:1)

棘手的部分是将您的列表拆分为任何值,因此我从here获取此部分。
然后我已经解析了列表部分以构建{{1 dict

res

答案 1 :(得分:1)

我编写了一个使用生成器的答案。这个想法是每次启动id令牌时,生成器返回计算的最后一个键。您可以通过更改check_fun()以及如何混合说明的一部分来进行成本核算。

def trailing_carriage(s):
    if s.endswith('\n'):
        return s[:-1]
    return s

def check_fun(s):
    """
    :param s:Take a string s
    :return: None if s dosn't match the ID rules. Otherwise return the
    name,value of the token
    """
    if ' ' in s:
        id_candidate,name = s.split(" ",1)
        try:
            return trailing_carriage(name),int(id_candidate)
        except ValueError:
            pass


def parser_list(list, check_id_prefix=check_fun):
    name = None #key dict
    id_candidate = None
    desc = "" #description string
    for token in list:
        check = check_id_prefix(token)
        if check is not None:
            if name is not None:
                """Return the previous coputed entry"""
                yield name,id_val,desc
            name,id_val = check
        else:
            """Append the description"""
            desc += trailing_carriage(token)
    if name is not None:
        """Flush the last entry"""
        yield  name,id_val,desc


>>> list = ['19841018 ID1\n', ' Plunging oil... \n', 'cut in the price \n', '\n', '19841018 ID2\n', ' The U.S. dollar...  \n', 'the foreign-exchange markets \n', 'late New York trading \n', '\n']
>>> print {k:[i,d] for k,i,d in parser_list(list)}
{'ID2': [19841018, ' Plunging oil... cut in the price  The U.S. dollar...  the foreign-exchange markets late New York trading '], 'ID1': [19841018, ' Plunging oil... cut in the price ']}