python json读取与解析
url = 'https://auction.jd.com/getJudicatureList.html? callback=jQuery8588604&page=1&limit=40&childrenCateId=13809&sortField= 3&paimaiStatus=2&_=1524121449636' html = download(url) json_file = re.findall(r"(?<=\"ls\"\:)[\w\W]*?(?=\,\"total\")",html)[0] json_data = json.loads(json_file,encoding='utf-8') #print(json_data) # with open('hhhhhh.json', 'w',encoding='utf-8') as fp: # json.dump(json_data, fp=fp, ensure_ascii=False, indent=4) with open('hhhhhh.json','r',encoding='utf-8') as fp: temp = json.loads(fp.read()) # print(type(temp)) for i in temp: print(i)
这里想把标准的json格式的内容保存到json文件中,但是正则匹配后是一个str形式,因此需要先用json的loads方法把字符串转为json格式,再写入json文件,然后读取的文件的时候,输出就是标准的json格式,要注意的地方是,这里要加上encoding='utf-8'
除此之外,还要在文件头部加上
# -*-conding:utf-8 -*-
人生苦短,何不用python