我有以下json
`{
"count": 4,
"node": [{
"assetRecord": {
"additionalhardware": null,
"admin": null,
"assetNumber": null,
"autoenable": null,
"building": null,
"category": "Unspecified",
"circuitId": null,
"comment": null,
"connection": null,
"cpu": null,
"dateInstalled": null,
"department": null,
"description": null,
"displayCategory": null,
"division": null,
"enable": null,
"floor": null,
"hdd1": null,
"hdd2": null,
"hdd3": null,
"hdd4": null,
"hdd5": null,
"hdd6": null,
"id": 10817,
"inputpower": null,
"lastModifiedBy": "",
"lastModifiedDate": 1491855810758,
"lease": null,
"leaseExpires": null,
"maintContractExpiration": null,
"maintContractNumber": null,
"maintcontract": null,
"managedObjectInstance": null,
"managedObjectType": null,
"manufacturer": null,
"modelNumber": null,
"notifyCategory": null,
"numpowersupplies": null,
"operatingSystem": null,
"password": null,
"pollerCategory": null,
"port": null,
"rack": null,
"rackunitheight": null,
"ram": null,
"region": null,
"room": null,
"serialNumber": null,
"slot": null,
"snmpcommunity": null,
"storagectrl": null,
"supportPhone": null,
"thresholdCategory": null,
"username": null,
"vendor": null,
"vendorAssetNumber": null,
"vendorFax": null,
"vendorPhone": null,
"vmwareManagedEntityType": null,
"vmwareManagedObjectId": null,
"vmwareManagementServer": null,
"vmwareState": null,
"vmwareTopologyInfo": null
},
"categories": [],
"createTime": 1491855810758,
"foreignId": null,
"foreignSource": null,
"id": "564",
"label": "10.21.204.174",
"labelSource": "A",
"lastCapsdPoll": 1491855810984,
"type": "A"
}, {
"assetRecord": {
"additionalhardware": null,
"admin": null,
"assetNumber": null,
"autoenable": null,
"building": null,
"category": "Unspecified",
"circuitId": null,
"comment": null,
"connection": null,
"cpu": null,
"dateInstalled": null,
"department": null,
"description": null,
"displayCategory": null,
"division": null,
"enable": null,
"floor": null,
"hdd1": null,
"hdd2": null,
"hdd3": null,
"hdd4": null,
"hdd5": null,
"hdd6": null,
"id": 10589,
"inputpower": null,
"lastModifiedBy": "",
"lastModifiedDate": 1490819238331,
"lease": null,
"leaseExpires": null,
"maintContractExpiration": null,
"maintContractNumber": null,
"maintcontract": null,
"managedObjectInstance": null,
"managedObjectType": null,
"manufacturer": null,
"modelNumber": null,
"notifyCategory": null,
"numpowersupplies": null,
"operatingSystem": null,
"password": null,
"pollerCategory": null,
"port": null,
"rack": null,
"rackunitheight": null,
"ram": null,
"region": null,
"room": null,
"serialNumber": null,
"slot": null,
"snmpcommunity": null,
"storagectrl": null,
"supportPhone": null,
"thresholdCategory": null,
"username": null,
"vendor": null,
"vendorAssetNumber": null,
"vendorFax": null,
"vendorPhone": null,
"vmwareManagedEntityType": null,
"vmwareManagedObjectId": null,
"vmwareManagementServer": null,
"vmwareState": null,
"vmwareTopologyInfo": null
},
"categories": [],
"createTime": 1490819238331,
"foreignId": null,
"foreignSource": null,
"id": "561",
"label": "10.21.205.148",
"labelSource": "A",
"lastCapsdPoll": 1490819238557,
"type": "A"
}, {
"assetRecord": {
"additionalhardware": null,
"admin": null,
"assetNumber": null,
"autoenable": null,
"building": null,
"category": "Unspecified",
"circuitId": null,
"comment": null,
"connection": null,
"cpu": null,
"dateInstalled": null,
"department": null,
"description": null,
"displayCategory": null,
"division": null,
"enable": null,
"floor": null,
"hdd1": null,
"hdd2": null,
"hdd3": null,
"hdd4": null,
"hdd5": null,
"hdd6": null,
"id": 7554,
"inputpower": null,
"lastModifiedBy": "",
"lastModifiedDate": 1477563144159,
"lease": null,
"leaseExpires": null,
"maintContractExpiration": null,
"maintContractNumber": null,
"maintcontract": null,
"managedObjectInstance": null,
"managedObjectType": null,
"manufacturer": null,
"modelNumber": null,
"notifyCategory": null,
"numpowersupplies": null,
"operatingSystem": null,
"password": null,
"pollerCategory": null,
"port": null,
"rack": null,
"rackunitheight": null,
"ram": null,
"region": null,
"room": null,
"serialNumber": null,
"slot": null,
"snmpcommunity": null,
"storagectrl": null,
"supportPhone": null,
"thresholdCategory": null,
"username": null,
"vendor": null,
"vendorAssetNumber": null,
"vendorFax": null,
"vendorPhone": null,
"vmwareManagedEntityType": null,
"vmwareManagedObjectId": null,
"vmwareManagementServer": null,
"vmwareState": null,
"vmwareTopologyInfo": null
},
"categories": [],
"createTime": 1477563144159,
"foreignId": null,
"foreignSource": null,
"id": "462",
"label": "10.21.50.153",
"labelSource": "A",
"lastCapsdPoll": 1490722710498,
"type": "A"
}, {
"assetRecord": {
"additionalhardware": null,
"admin": null,
"assetNumber": null,
"autoenable": null,
"building": null,
"category": "Unspecified",
"circuitId": null,
"comment": null,
"connection": null,
"cpu": null,
"dateInstalled": null,
"department": null,
"description": null,
"displayCategory": null,
"division": null,
"enable": null,
"floor": null,
"hdd1": null,
"hdd2": null,
"hdd3": null,
"hdd4": null,
"hdd5": null,
"hdd6": null,
"id": 5119,
"inputpower": null,
"lastModifiedBy": "",
"lastModifiedDate": 1461419153008,
"lease": null,
"leaseExpires": null,
"maintContractExpiration": null,
"maintContractNumber": null,
"maintcontract": null,
"managedObjectInstance": null,
"managedObjectType": null,
"manufacturer": null,
"modelNumber": null,
"notifyCategory": null,
"numpowersupplies": null,
"operatingSystem": null,
"password": null,
"pollerCategory": null,
"port": null,
"rack": null,
"rackunitheight": null,
"ram": null,
"region": null,
"room": null,
"serialNumber": null,
"slot": null,
"snmpcommunity": null,
"storagectrl": null,
"supportPhone": null,
"thresholdCategory": null,
"username": null,
"vendor": null,
"vendorAssetNumber": null,
"vendorFax": null,
"vendorPhone": null,
"vmwareManagedEntityType": null,
"vmwareManagedObjectId": null,
"vmwareManagementServer": null,
"vmwareState": null,
"vmwareTopologyInfo": null
},
"categories": [],
"createTime": 1461419153008,
"foreignId": null,
"foreignSource": null,
"id": "389",
"label": "dummyname_xv1",
"labelSource": "A",
"lastCapsdPoll": 1490722710502,
"type": "A"
}],
"totalCount": 271
}
上面的代码实际上是转储到一个文件(例如abc.txt)中,它是一种有效的JSON格式。我需要打开文件并遍历“标签”字段。 我需要编写一个代码,只有当它是IP,即格式为0.0.0.0
时才应提取/打印“label”值。怎么做?
答案 0 :(得分:3)
尝试使用json
模块解析json文件并迭代node
:
反序列化fp(.read() - 支持包含文件的文件对象 JSON文档)使用此转换表的Python对象。
如果要验证IP字符串是否有效,可以使用inet_aton ()。
从点分四字符串格式转换IPv4地址(例如, '123.45.67.89')以32位打包二进制格式,作为字符串四 字符的长度。
import json
import socket
for i in json.load(open('ooo.json'))["node"]:
try:
socket.inet_aton(i["label"])
print(i["label"])
except Exception as e:
pass
结果:
10.21.204.174
10.21.205.148
10.21.50.153