如何用Python,查找json格式中指定的数据,然后输出这些查找到的数据?
{"aescCityList":null,"cityAllList":null,"cityJsonArray":[{"cityId":3,"cityName":"上海","internetPoints":{"cityId":0,"color":"","curravgvalue":0,"districtid":0,"internetValues":{"aqi":0.0,"co":0.0,"co_24h":0.0,"no2":0.0,"no2_24h":0.0,"o3":0.0,"o3_24h":0.0,"o3_8h":0.0,"o3_8h_24h":0.0,"pm10":0.0,"pm10_24h":0.0,"pm2_5":0.0,"pm2_5_24h":0.0,"pointid":0,"so2":0.0,"so2_24h":0.0,"updatetime":""},"pointName":"","pointid":0,"pointname":"","preavgvalue":0,"stationCode":"","updateTime":"","value":0,"xvalue":0.0,"yvalue":0.0},"pinyin":"shanghai","updateTime":"2013-12-10 14:13:56.0"},{"cityId":4,"cityName":"东莞","internetPoints":{"cityId":0,"color":"","curravgvalue":0,"districtid":0,"internetValues":{"aqi":0.0,"co":0.0,"co_24h":0.0,"no2":0.0,"no2_24h":0.0,"o3":0.0,"o3_24h":0.0,"o3_8h":0.0,"o3_8h_24h":0.0,"pm10":0.0,"pm10_24h":0.0,"pm2_5":0.0,"pm2_5_24h":0.0,"pointid":0,"so2":0.0,"so2_24h":0.0,"updatetime":""},"pointName":"","pointid":0,"pointname":"","preavgvalue":0,"stationCode":"","updateTime":"","value":0,"xvalue":0.0,"yvalue":0.0},"pinyin":"dongguan","updateTime":"2013-12-10 14:13:56.0"},{"cityId":5,"cityName":"东营","internetPoints":{"cityId":0,"color":"","curravgvalue":0,"districtid":0,"internetValues": 展开
用Python查找json格式中指定的数据输出这些查找到的数据的操作步骤如下:
1,打开一个编辑器,例如sublime text 3,然后创建一个新的PY文档。
2,导入os因为要传输文件,所以我们必须引入os库,以便您可以找到文件。
3,用打开(“ E:\\ test.txt”)作为文件:print(file.readlines())在这里读取文件,可以看到每一行都以列表的形式存储。
4,r l行:l = l.strip(“ \ n”)print(l)在这里,我们使用strip删除换行符。
5,string = l.split(“ =”)接下来,我们使用split将字符串拆分为一个列表。
6,创建一个空字典。结果[string [0]] = string [1]现在将每行的键和值导入字典。
1、打开编辑器,例如sublime text 3,新建一个PY文档。
2、import os因为要调入文件,那么我们要引入os库,这样可以定位文件。
3、with open("E:\\test.txt") as file:print(file.readlines())这里读取一下文件,可以看出每一行是由列表的形式存储的。
4、r l in lines: l = l.strip("\n") print(l)这里我们利用strip来去除掉换行符。
5、string = l.split("=")接下来我们用split把字符串拆分为列表。
6、新建一个空的字典。result[string[0]] = string[1]现在把每一行的键和值都导入到字典里面。
可以首先使用json包的loads函数对json数据进行解析,然后就可以像操作Python数据格式一样对数据进行索引和遍历了。
import json
s = '{"aescCityList":null,"cityAllList":null,"cityJsonArray"...'
data = json.loads(s)
for city in data["cityJsonArray"]:
if city["cityId"] == 4:
print city
2015-07-14
js = js.loads(str)
for city in js['cityJsonArray']:
print city['cityId']
2015-07-17