【发布时间】:2018-07-17 15:12:39
【问题描述】:
这是我的json文件的结构
},
"client1": {
"description": "blabla",
"contact name": "",
"contact email": "",
"third party organisation": "",
"third party contact name": "",
"third party contact email": "",
"ranges": [
"1.1.1.1",
"2.2.2.2",
"3.3.3.3"
]
},
"client2": {
"description": "blabla",
"contact name": "",
"contact email": "",
"third party organisation": "",
"third party contact name": "",
"third party contact email": "",
"ranges": [
"4.4.4.4",
"2.2.2.2"
]
},
我已经看到了导出这个 json 文件的特定部分的方法,但不是全部。基本上我想做的就是使用用户输入搜索文件。 我所苦苦挣扎的是我如何实际使用用户输入来根据输入搜索和打印 client1 或 client2 下的所有内容?我确信这只是 1 或 2 行代码,但无法弄清楚。蟒蛇新手。这是我的代码
data = json.load(open('clients.json'))
def client():
searchq = input('Client to export: '.capitalize())
search = ('""'+searchq+'"')
a = open('Log.json', 'a+')
a.write('Client: \n')
client()
【问题讨论】:
-
您到底想搜索什么?你能举一个查询的例子和想要的结果吗?
-
我希望能够搜索“client1”,并将其存储在一个可以附加到新文件的变量中,它应该包括 client1 下的所有字段,感谢我能得到的任何帮助! !
-
所以您希望用户输入“client1”,然后从 JSON 中获取“client1”的信息?
-
查看python文档处理jsondocs.python.org/2/library/json.html