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 -*-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM