繁体   English   中英

如何更新字典以在python中获得所需的输出?

[英]How to update the dictionary to get the desired ouput in python?

我需要这种格式,但我想我对字典没有更多了解,我有文本文件,我想将文本文件转换为以下格式:

gameConfiguration.txt

#Rooms
room Hall
room Kitchen
room Storage
room Bedroom
room Bathroom
room Garage
#Doors
door N open Hall Bedroom
door N closed Storage Kitchen
door E locked Hall Storage
door E open Bedroom Kitchen
door N open Bedroom Bathroom
door S open Bedroom Hall
door S open Bathroom Bedroom
door W locked Hall Garage
door W open Kitchen Bedroom
door S closed Kitchen Storage
door W open Storage Hall
door E locked Garage Hall
#Items
item car Garage STATIONARY
item remote Kitchen MOVE
item key Bedroom USE
item key Hall USE
#Start position
start Hall

字典

rooms={"Hall":{"name":"Hall","N":{"dir":"Bedroom","status":"open"},"E":{"dir":"Storage","status":"locked"},"W":{"dir":"Garage","status":"locked"},"directions":["N","W","E"],"item":"key"},
           "Bedroom":{"name":"Bedroom","E":{"dir":"Kitchen","status":"open"},"N":{"dir":"Bathroom","status":"open"},"S":{"dir":"Hall","status":"open"},"directions":["E","N","S"],"item":"key","start":1},
           "Storage":{"name":"Storage","N":{"dir":"Kitchen","status":"closed"},"W":{"dir":"Hall","status":"open"},"directions":["N","W"],"start":"Hall"},
           "Garage":{"name":"Garage","E":{"dir":"Hall","status":"locked"},"directions":["E"],"item":"car","start":"Hall"},
           "Kitchen":{"name":"Kitchen","W":{"dir":"Bedroom","status":"open"},"S":{"dir":"Storage","status":"closed"},"directions":["W","S"],"item":"remote","start":"Hall"},
           "Bathroom":{"name":"Bathroom","S":{"dir":"Bedroom","status":"open"},"directions":["S"],"start":"Hall"}}

我忽略了那些以#开头的行,并将行拆分为单词,并尝试将我真正想要的内容转换为字典。

我的代码:

d={}
def dictnPart():
    with open("gameconfiguration.txt",'r') as input_data:
        for line in input_data:
            li=line.strip()
            if not li.startswith("#"):
                abc=line.strip().split()
                # print(abc[1])
                if abc[0]=="room":
                    d={abc[1]:{"name":abc[1]}}
                    # print(d)
                if abc[0]=="door":
                    d.update({abc[1]:{"dir":abc[4],"status":abc[2]},"directions":[abc[1]]})
                    # print(d)
                if abc[0]=="item":
                    d.update({"item":abc[1],"directions":abc[2]})
                    print(d)
dictnPart()

但是主要的问题是它与最近的和我得到的重叠:

{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Garage', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'car'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Kitchen', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'remote'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Bedroom', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'key'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Hall', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'key'}

帮助我获得理想的输出。 请!!

dooritem的行上都有房间名称。 使用它来访问字典的适当元素。

d={}
def dictnPart():
    with open("gameconfiguration.txt",'r') as input_data:
        for line in input_data:
            li=line.strip()
            if not li.startswith("#"):
                abc=line.strip().split()
                # print(abc[1])
                if abc[0]=="room":
                    d[abc[1]] = {"name":abc[1]}
                    # print(d)
                if abc[0]=="door":
                    roomname = abc[3]
                    d[roomname].update({abc[1]:{"dir":abc[4],"status":abc[2]}})
                    if "directions" in d[roomname]:
                        d[roomname]["directions"].append(abc[1])
                    else:
                        d[roomname]["directions"] = [abc[1]]
                    # print(d)
                if abc[0]=="item":
                    roomname = abc[2]
                    d[roomname].update({"item":abc[1]})
                    if "directions" in d[roomname]:
                        d[roomname]["directions"].append(abc[2])
                    else:
                        d[roomname]["directions"] = [abc[2]]
                    print(d)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM