使用 json 文件中的特定键、值对更新 python 字典
Updating a python dictionary with specific key, value pairs from a json file
基本上,我想从 json 文件中获取特定的键和值,并将其附加到 python 中的字典中。到目前为止,我已经想出了如何将所有 json 数据附加到我的字典中,但这不是我想要的。
这是我的 json 文件,其中包含供用户添加到他们的库存中的项目,其中包含在检查时将显示的文本。
{
"egg" : "This smells funny.",
"stick" : "Good for poking!",
"frying pan" : "Time to cook."
}
我想要完成的是当用户拿起某个物品时,它会从 json 文件导入到 python 字典中作为他们的库存。
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory.update(dic)
x = 'egg'
addInventory(x)
print(inventory)
因此,当用户选择一个 'egg' 时,我想以某种方式从 json 文件中获取该键和值集,并将其附加到我在 python 中的清单字典中。我假设 for 循环可以解决这个问题,但我似乎无法弄明白。
试试这个:
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory.update(dic[args])
x = 'egg'
addInventory(x)
print(inventory)
import json
with open('examine.json', 'r') as f:
json_inventory = json.load(f)
def addInventory(x):
try:
my_inventory[x] = json_inventory[x]
except Exception as e:
logging.info("User trying to add something that is not in the json")
def removeInventory(x):
try:
my_inventory.pop(x)
except Exception as e:
logging.info("User trying to remove something that is not in the inventory")
my_inventory = {}
x = 'egg'
addInventory(x) #add to your inventory
print(my_inventory)
x = 'stick'
addInventory(x) #add to your inventory again
print(my_inventory)
x = 'egg'
removeInventory(x) #remove from your inventory
print(my_inventory)
您的代码无法运行的原因是您只是在做 inventory.update(f)
,因此它与创建 f
的副本一样好,就像 inventory
。如果您希望 inventory
仅包含特定 key
的 key:value
,则以下代码将起作用:
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory[args] = f[args]
x = 'egg'
addInventory(x)
print(inventory)
基本上,我想从 json 文件中获取特定的键和值,并将其附加到 python 中的字典中。到目前为止,我已经想出了如何将所有 json 数据附加到我的字典中,但这不是我想要的。
这是我的 json 文件,其中包含供用户添加到他们的库存中的项目,其中包含在检查时将显示的文本。
{
"egg" : "This smells funny.",
"stick" : "Good for poking!",
"frying pan" : "Time to cook."
}
我想要完成的是当用户拿起某个物品时,它会从 json 文件导入到 python 字典中作为他们的库存。
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory.update(dic)
x = 'egg'
addInventory(x)
print(inventory)
因此,当用户选择一个 'egg' 时,我想以某种方式从 json 文件中获取该键和值集,并将其附加到我在 python 中的清单字典中。我假设 for 循环可以解决这个问题,但我似乎无法弄明白。
试试这个:
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory.update(dic[args])
x = 'egg'
addInventory(x)
print(inventory)
import json
with open('examine.json', 'r') as f:
json_inventory = json.load(f)
def addInventory(x):
try:
my_inventory[x] = json_inventory[x]
except Exception as e:
logging.info("User trying to add something that is not in the json")
def removeInventory(x):
try:
my_inventory.pop(x)
except Exception as e:
logging.info("User trying to remove something that is not in the inventory")
my_inventory = {}
x = 'egg'
addInventory(x) #add to your inventory
print(my_inventory)
x = 'stick'
addInventory(x) #add to your inventory again
print(my_inventory)
x = 'egg'
removeInventory(x) #remove from your inventory
print(my_inventory)
您的代码无法运行的原因是您只是在做 inventory.update(f)
,因此它与创建 f
的副本一样好,就像 inventory
。如果您希望 inventory
仅包含特定 key
的 key:value
,则以下代码将起作用:
import json
inventory = {}
def addInventory(args):
f = open('examine.json', 'r')
dic = json.load(f)
inventory[args] = f[args]
x = 'egg'
addInventory(x)
print(inventory)