Python JSON common operations

Source: Internet
Author: User

JSON module (string manipulation)
    • Json.dumps (): Encode the data
    • Json.loads (): Decoding the data

      JSON module (file operation)
# 写入 JSON 数据with open('data.json', 'w') as f:    json.dump(data, f)# 读取数据with open('data.json', 'r') as f:    data = json.load(f)
Save As. json file
json_data={}with open('na.json','w',encoding='utf-8') as json_file:    json.dump(json_data,json_file,ensure_ascii=False)    print("json 文件保存成功! ")
Reading JSON files
import jsonwith open('na.json','r',encoding='utf-8') as json_file:    data=json.load(json_file)    print(data)
Beautiful printing of JSON files (functions can be copied directly)
import jsondef print_json():    js_data=open('na.json','r',encoding='utf-8')    js=json.load(js_data)    pretty_dict(js)def pretty_dict(my_dict): #美观打印    #利用json的打印 友好打印字典等结构。备用    print(json.dumps(my_dict,ensure_ascii=False,indent=1))if __name__=='__main__':    print_json()

Python JSON common operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.