JSON and Pickle in Python3

Source: Internet
Author: User

JSON and Pickle: functions primarily for file data serialization operations

1:json save data to file:

Import Jsoninfo = {    "name": "HJC",    "age": 22}with open ("A1.txt", "W", encoding= "Utf-8") as F:    F.write ( Json.dumps (Info))

  2.Json of data fetching

Import Jsonwith Open ("A1.txt", "R", encoding= "Utf-8") as f:    data = Json.loads (F.readline ())    print (data["age"])

  3.Pickle of stored data

Import Pickle def func ():    print ("Hello tomorrow!!!") info = {    "name": "HJC",    "age": $,    "hobby": func}w ITH open ("A1.txt", "WB") as F:    F.write (Pickle.dumps (info))

  Fetch data for 4.Pickle:

Import Pickle def func ():    print ("Hello Tomorrow!!!") with open ("A1.txt", "RB") as F:    data = Pickle.loads (F.read () ) data["hobby"] ()

*pickle reads and writes are binary, so use ' RB ' & ' WB '

#文章引用KaShing大神

JSON and Pickle in Python3

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.