我想从JSON文件中更新密钥(然后将其转换为python字典)。我希望在我的更新文件中有一个嵌套的字典,但我不知道怎么做。
f = dict(
source=result['sourcefile'],
destination=result['destinationfile']
)在这段代码中,我有result,它是我的JSON输出。我有密钥sourcefile和destinationfile是我从API中获得的密钥。我想将它们更改为source和destination。这段代码在这里完成了这项工作;但是,我希望我的字典是嵌套的(无论是使用list还是另一个dict)。如下所示:
{"F":{"source":"samplevalue","destination":"samplevalue"}}发布于 2017-01-07 05:12:38
以下是示例代码,其中包含了您所展示的代码并生成了您所展示的JSON。它只是按照描述生成对象,并将其编码为JSON。
import json
result = {'sourcefile': "samplevalue", 'destinationfile':"samplevalue"}
f = dict(
source=result['sourcefile'],
destination=result['destinationfile']
)
g = {"F": f}
print( json.dumps(g) )https://stackoverflow.com/questions/41513939
复制相似问题