JSON and Pickle modules

Source: Internet
Author: User

# JSON module # import json# dumps method: # JY = {' name ': ' Chinese ', ' price ': 400}# date = Json.dumps (JY) # converts JY to a string that all languages can recognize # with open (' Li Bai ', ' W ', encoding= ' UTF8 ') as f: # write ' Li Bai ' document # F.write (date) # loads method: # with open (' Li Bai ', ' r ', encoding= ' UTF8 ') as f:# date = j Son.loads (F.read ()) # read # print (date) # {' Name ': ' Zhonghua ', ' price ': 400}# print (date[' name ') ') # Chinese # Dump method: # JY = {' Name ': ' Zhonghua ', ' price ': 4000}## with open (' Li Bai ', ' W ', encoding= ' UTF8 ') as f:# date = Json.dump (jy,f) # Write, the dump method omits the F.write () method # Lo Ad method: # with open (' Li Bai ', ' r ', encoding= ' UTF8 ') as f:# date = Json.load (f) # Read, the Load method omits the F.read () method # # print (date) # {' name ': ' Chinese ', ' price ': 4000}# print (date[' price ') # 4000# pickle Module # like JSON, just replace JSON with Pickle, JSON is a string converted to all languages, and Pickle is a conversion    String # JSON known as Python cannot convert functions, classes, and Pickle can import pickle# def foo (): # print (' OK ') # Dump method: # with open (' Li Bai ', ' WB ') as F: # pickle needs to write bytes, so use wb# date = Pickle.dump (foo,f) # Load method: # with open (' Li Bai ', ' RB ') as f:# date = Pickle.load (f) # # D Ate () #ok # The function read has noIn the current memory, the Foo function needs to be re-written to the current document, which is seldom used 

JSON and pickle modules

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.