我已经学习了几个月的python,在我自己的项目中开始了几个在线课程。我发现了一个有趣的网站,它以json格式提供数据,我使用urllib库导入,使用json库解析并最终将其插入到mysql数据库中。这个项目的第一部分和最后一部分工作,但不是解析部分。这是完整的json文件的一部分:
{
"total": 24555,
"results": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": null,
"id": "556897c8b9a2143b1187cf79",
"properties": {
"Expedition": "okavango_15",
"SpeciesName": "African Hoopoe",
"Count": 1,
"t_utc": 1325383950,
"t_created": 1432917960.344582,
"Member": "Maans",
"Taxonomy": {
"Kingdom": "Animalia",
"Species": "Upupa epops",
"Family": "Upupidae",
"Genus": "Upupa",
"Phylum": "Chordata",
"Order": "Upupiformes",
"Class": "Aves"
},
"FeatureType": "sighting",
"Images": [],
"DateTime": "2012-01-01T04:12:30+0200",
"EstimatedGeometry": null,
"Behavior": "Flying",
"Habitat": "Mioambo Woodland"
}
},
{
"type": "Feature",
"geometry": null,
"id": "556897b2b9a2143b1d14a807",
"properties": {
"Expedition": "okavango_15",
"SpeciesName": "Peregrine Falcon",
"Count": 1,
"t_utc": 1325406263,
"t_created": 1432917938.878641,
"Member": "Maans",
"Taxonomy": {
"Kingdom": "Animalia",
"Species": "Falco peregrinus",
"Family": "Falconidae",
"Genus": "Falco",
"Phylum": "Chordata",
"Order": "Falconiformes",
"Class": "Aves"
},
"FeatureType": "sighting",
"Images": [],
"DateTime": "2012-01-01T10:24:23+0200",
"EstimatedGeometry": null,
"Behavior": "Flying",
"Habitat": "Mioambo Woodland"
}
},
{
"properties": {
"SpeciesName": "Reed Cormorant",
"Count": 1,
"Accuracy": 8.0,
"Expedition": "okavango_13",
"t_created": 1430929467.352914,
"Member": null,
"FeatureType": "sighting",
"Altitude": 973.2047942238661,
"t_utc": 1378449600,
"DateTime": "2013-09-06T08:40:00+0200",
"Activity": "L",
"SightingId": 0
},
"geometry": {
"type": "Point",
"coordinates": [
22.345429148371103,
-19.018043140479318
]
},
"id": "554a403bb9a21401a231284f",
"type": "Feature"
}
]
},
"resolution": "full",
"filter": {
"FeatureType": "sighting"
},
"returned": 3,
"order": 1,
"limit": 3
}
为解析json文件,我使用以下代码:
import json
def remove_nulls(d):
return {k: v for k, v in d.iteritems() if v is not None}
with open('okavango.json') as fhand:
data = json.loads(fhand.read(), object_hook=remove_nulls)
我想从中获取数据的级别是"功能",其中包含动物目击以及许多其他键,如物种名称,时间戳,位置。各种目击不总是包含相同的键,例如位置并不总是存在。我想通过瞄准思想来解析数据目标,因为我希望它在我的数据库中是一行。通过使用这种代码调用每个单独的项目,我能够获得我想要的每一个数据:
results = data["results"]
for item in results["features"]:
value1 = item["id"]
value2 = item["properties"]["t_created"]
但是这使得我的代码很长,而且我猜它应该更容易迭代。但是,我无法获得每个单个的单键/值结果。我已经使用了.get函数,.values等但事实证明,我的json文件中的每个级别都不是字典,而是列表。如果我运行此代码,例如:
print type(data)
data2 = data["results"]
print type(data2)
data3 = data2["features"]
print type(data3)
我的结果是这样的: 键入' dict' 键入' dict' 键入' list'
如何迭代原始json加载并通过目击解析数据?
答案 0 :(得分:1)
我不明白你的意思"通过瞄准"解析数据,但以下代码将遍历你的JSON。如果您调整问题以包含您希望的输出示例,我可以提供更多帮助。
data = json.loads(json_data)
def iterate_item(item):
result_str = ""
if type(item) == dict:
for key in item:
result_str = result_str + key + ": " + iterate_item(item[key])
elif type(item) == list:
for i in item:
result_str = result_str + iterate_item(i) + ", "
else:
if not item:
item = "None"
result_str = result_str + str(item) + ". "
return result_str
for sighting in data["results"]["features"]:
print iterate_item(sighting) + "\n\n"