您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 将一行分成具有多层键值对的字典

5b51 2022/1/14 8:23:33 python 字数 3476 阅读 594 来源 www.jb51.cc/python

我有一个包含这种格式的行的文件.Example 1: nextline = 'DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; Score = { Player1 = 4 };' Example 2: nextline = 'DD:MM:YYYY IN

概述

我有一个包含这种格式的行的文件.

Example 1:
nextline = "DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; score = { Player1 = 4 };"

Example 2:
nextline = "DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; score = { Player1 = 4; Player2 = 6; Player3 = 4 };"

我首先用’:’分割这一行,这给了我一个包含2个条目的列表.
我想将这一行拆分为带有键和值的字典,但是得分键有多个具有值的子键.

Hole 1
Par 4
Index 2
Distance 459
score 
    Player1 4
    Player2 6
    Player3 4

所以我使用的是这样的……

split_line_by_semicolon = nextline.split(":")
dictionary_of_line = dict((k.strip(),v.strip()) for k,v in (item.split('=')     
    for item in split_line_by_semicolon.split(';')))
        for keys,values in dictionary_of_line.items():
            print("{0} {1}".format(keys,values))

但是我在该行的得分元素上收到错误

ValueError: too many values to unpack (expected 2)

我可以将’=’上的分割调整为此值,因此它会在第一个’=’后停止

dictionary_of_line = dict((k.strip(),v in (item.split('=',1)     
    for item in split_line_by_semicolon.split(';')))
        for keys,values))

但是我丢失了大括号内的子值.有谁知道如何实现这个多层字典?

import re

nextline = "DD:MM:YYYY INFO - 'WeeklyMedal: Hole = 1; Par = 4; Index = 2; Distance = 459; score = { Player1 = 4; Player2 = 6; Player3 = 4 };"

# compiles the regular expression to get the info you want
my_regex = re.compile(r'\w+ \= \w+')

# builds the structure of the dict you expect to get 
final_dict = {'Hole':0,'Par':0,'Index':0,'Distance':0,'score':{}}

# uses the compiled regular expression to filter out the info you want from the string
filtered_items = my_regex.findall(nextline)

for item in filtered_items:
    # for each filtered item (string in the form key = value)
    # splits out the 'key' and handles it to fill your final dictionary
    key = item.split(' = ')[0]
    if key.startswith('Player'):
        final_dict['score'][key] = int(item.split(' = ')[1])
    else:
        final_dict[key] = int(item.split(' = ')[1])

总结

以上是编程之家为你收集整理的python – 将一行分成具有多层键值对的字典全部内容,希望文章能够帮你解决python – 将一行分成具有多层键值对的字典所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶