用Python在Json Data中创建数据集

时间:2018-01-11 13:51:15

标签: python json combobox

我的项目中有一个ttk ComboBox,如下所示:

var = tkinter.StringVar()
var.set('Actually Additions Atomic Reconstructor')
p = ttk.Combobox(window, textvariable = var, values = data, justify='center')
p.bind('<<ComboboxSelected>>', box_text)
p.config(font='Helvetica 12 bold', width="30")
p.pack(side=TOP)

然后是一个包含如下项目的数据集:

data={
'Advanced Mortars',
'Artisan Worktables',
'Initial Inventory',
'JEI Hide'
}

等。

但是我现在想知道如何能够像我制作的json文件一样:

[{
"title": "Advanced Mortars",
"import": "import mods.advancedmortars.Mortar;"
}, {
"title": "Initial Inventory",
"import": "import mods.initialinventory.InvHandler;"
}, {
"title": "Artisan Worktables",
"import": "import mods.artisanworktables.Worktable;"
}, {
"title": "JEI Hide",
"import": "mods.jei.JEI.hide;"
}]

然后在JSON文件中进一步获取这些“title”值,并基本上用它们替换数据变量。我希望这是有道理的,像这样:

data = {titles in json file}

所以我可以在我去的时候添加到Json文件

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

import json

with open('file.json', 'r+') as f:
    content = json.load(f)

data = [[i['title'] for i in content]

data变量将为['Advanced Mortars', 'Initial Inventory', 'Artisan Worktables', 'JEI Hide']