Json&pickle Module of Python basics

Source: Internet
Author: User
Tags serialization

JSON & Pickle Modules
  1. JSON modules: Only for simple data types, is a cross-platform module.

    Let's say we have a piece of data that defines this:

    = {    ‘name‘:‘alex‘,    ‘age‘:22}
    For this simple data type, we can directly use theJsonFor serialization and deserialization of data:
      • Serialization dumps :

        import=open("test.text","wb")f.write(json.dumps(info))f.close()
        Similarly, we can use dump methods instead of dumps methods:
        You only need tof.write(json.dumps(info))
        Replace json.dump(info,f) it with.
      • Deserialization loads :

        import=open("test.txt","rb"= json.loads(f.read())

        Similarly, we can use load methods instead of loads methods:
        You only need tof.write(json.dumps(info))
        Replace json.dump(info,f) it with.

  2. Pickle module: Capable of converting complex data types, is a unique data type of Python.

    Through the JSON module above we find that JSON can only pass some simple data types, if we want to info add a function?

    def sayhi(name):    print("hello2,"= {‘name‘:‘alex‘,‘age‘:22‘func‘:sayhi    }
    This time, we need to use our pickle module.
    • Serialization dumps :

      import=open("test.text","wb")f.write(json.pickle(info))f.close()
      Similarly, we can use dump methods instead of dumps methods:
      You only need tof.write(pickle.dumps(info))
      Replace pickle.dump(info,f) it with.
    • Deserialization loads :

      import pickledef sayhi(name):    print("hi"+=open("test.txt","rb"= pickle.loads(f.read())

      Similarly, we can use load methods instead of loads methods:
      You only need tof.write(picklejson.dumps(info))
      Replace pickle.dump(info,f) it with.

      It is important to note that the function needs to be redefined when deserializing the session sayhi() .

  3. Precautions:

    When serializing and deserializing data using the JSON module and the Pickle module, it is a dump-to- load principle , which is to dump only one copy and load only one copy.

Json&pickle Module of Python basics

Related Article

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.