在 Python 中从 JSON 数据创建列表和字典 [重复]
Posted
技术标签:
【中文标题】在 Python 中从 JSON 数据创建列表和字典 [重复]【英文标题】:Creating lists and dictionaries from JSON data in Python [duplicate] 【发布时间】:2014-07-25 22:13:56 【问题描述】:我正在编写一个脚本来通过 REST 收集一些库存数据。然后我想过滤它以创建一个列表和两个字典,我可以在脚本的其他地方使用它们。
例如,从这个:
'version': '0.0'
'response': ['chassisType': 'C800',
'family': 'C897VA-K9',
'hostname': 'chaney-xtr',
'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin',
'interfaceCount': '10',
'lastUpdated': '2014-06-03 01:39:19.855491-07',
'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051',
'macAddress': '88:5A:92:A4:E7:C8',
'managementIpAddress': '192.168.2.1',
'memorySize': '988236K/60339K',
'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a',
'numUpdates': 1,
'platformId': 'C897VA-K9',
'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1',
'role': 'Unknown',
'roleSource': 'auto',
'serialNumber': 'FGL175124DX',
'softwareVersion': '15.4(2)T',
'type': 'UNKNOWN',
'upTime': '2 weeks, 3 days, 18 hours, 2 minutes',
'vendor': 'Cisco',
'chassisType': 'C800',
'family': 'C897VA-K9',
'hostname': 'chaney-xtr2',
'imageName': 'c800-universalk9-mz.SPA.154-2.T.bin',
'interfaceCount': '10',
'lastUpdated': '2014-06-03 01:39:19.855491-07',
'lineCardId': 'e5bddd56-2194-4b83-8ae5-597893800051',
'macAddress': '88:5A:92:A4:E7:C8',
'managementIpAddress': '192.168.2.2',
'memorySize': '988236K/60339K',
'networkDeviceId': 'e15789bd-47df-4df9-809f-daf81d15ff2a',
'numUpdates': 1,
'platformId': 'C897VA-K9',
'portRange': 'ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1',
'role': 'Unknown',
'roleSource': 'auto',
'serialNumber': 'XGL175124D3',
'softwareVersion': '15.4(2)T',
'type': 'UNKNOWN',
'upTime': '2 weeks, 3 days, 18 hours, 2 minutes',
'vendor': 'Cisco'],
其中"platformId" = "C897VA-K9"
我想从managementIpAddress
创建一个IP 地址列表
还有两个以IP地址为key的字典
dict1 = "managementIpAddress": "hostname"
dict2 = "managementIpAddress": "platformId"
你会怎么做呢?
亲切的问候,
瑞恩
【问题讨论】:
你尝试过哪些方法? 你试过python文档吗? docs.python.org/2/library/json.html 正如@cwallenpoole 提到的,您应该分享到目前为止您尝试过的事情,以及您遇到的具体问题。 【参考方案1】:from_this = "version":"0.0","response":["macAddress":"88:5A:92:A4:E7:C8","networkDeviceId":"e15789bd-47df-4df9-809f-daf81d15ff2a","lineCardId":"e5bddd56-2194-4b83-8ae5-597893800051","lastUpdated":"2014-06-03 01:39:19.855491-07","platformId":"C897VA-K9","vendor":"Cisco","numUpdates":1,"interfaceCount":"10","portRange":"ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1","roleSource":"auto","chassisType":"C800","softwareVersion":"15.4(2)T","upTime":"2 weeks, 3 days, 18 hours, 2 minutes","imageName":"c800-universalk9-mz.SPA.154-2.T.bin","memorySize":"988236K/60339K","managementIpAddress":"192.168.2.1","family":"C897VA-K9","type":"UNKNOWN","serialNumber":"FGL175124DX","role":"Unknown","hostname":"chaney-xtr", "macAddress":"88:5A:92:A4:E7:C8","networkDeviceId":"e15789bd-47df-4df9-809f-daf81d15ff2a","lineCardId":"e5bddd56-2194-4b83-8ae5-597893800051","lastUpdated":"2014-06-03 01:39:19.855491-07","platformId":"C897VA-K9","vendor":"Cisco","numUpdates":1,"interfaceCount":"10","portRange":"ATM0, ATM0.1, BRI0, BRI0:1-2, Dialer1, Ethernet0, GigabitEthernet0-8, LISP0, Loopback0-1, NVI0, Virtual-Access1, Virtual-Template1, Vlan1","roleSource":"auto","chassisType":"C800","softwareVersion":"15.4(2)T","upTime":"2 weeks, 3 days, 18 hours, 2 minutes","imageName":"c800-universalk9-mz.SPA.154-2.T.bin","memorySize":"988236K/60339K","managementIpAddress":"192.168.2.2","family":"C897VA-K9","type":"UNKNOWN","serialNumber":"XGL175124D3","role":"Unknown","hostname":"chaney-xtr2"]
dict1 =
dict2 =
for response_item in from_this['response']:
dict1[response_item['managementIpAddress']] = response_item['hostname']
dict2[response_item['managementIpAddress']] = response_item['platformId']
输出如下:
In[189]: dict1
Out[187]: '192.168.2.1': 'chaney-xtr', '192.168.2.2': 'chaney-xtr2'
In[190]: dict2
Out[188]: '192.168.2.1': 'C897VA-K9', '192.168.2.2': 'C897VA-K9'
如果这是您要找的,如果您有任何问题,请告诉我。
【讨论】:
哇!那很快。谢谢Krishan,这很有帮助。对于那些询问的人,我发现 Python 文档很难理解,但是我确实在这个网站上找到了这个答案,这也很有帮助:***.com/questions/23024477/…。【参考方案2】:你可以解析json数据,然后添加一些if和else case来判断是否需要数据;然后使用eval语句生成一个dict
【讨论】:
如果你扩展它并提供示例代码,你的答案会更清楚以上是关于在 Python 中从 JSON 数据创建列表和字典 [重复]的主要内容,如果未能解决你的问题,请参考以下文章