Module-json,pickle

Source: Internet
Author: User

#JSON pickle shelve module serialization#JSON modules are used for data transfer between different languages, but the qualification data type: Str,int,list,dict, BooleanImportJSON#Four ways:#dumps and loads: For network data transfer and multiple data writes to a fileDict = {'wo':'Good_person','ni':'Bad_person'}ret= Json.dumps (Dict,ensure_ascii=false)#{"Wo": "Good_person", "ni": "Bad_person"}#convert Dict to serialized string Ensure_ascii=false: Chinese visual of StringPrint(ret)#deserializationPrint(Json.loads (ret)) Dict1= {'wo':'Good_person','ni':'Bad_person'}dict2= {'wo':'Good_person','ni':'Bad_person'}dict3= {'wo':'Good_person','ni':'Bad_person'}#multiple data written in one fileWith open ('Json_files','W', encoding='Utf-8') as F:ret1= Json.dumps (dict1,ensure_ascii=False) f.write (Ret1+'\ n') Ret2= Json.dumps (dict2,ensure_ascii=False) f.write (Ret2+'\ n') Ret3= Json.dumps (dict3,ensure_ascii=False) f.write (Ret3+'\ n') with open ('Json_files','R', encoding='Utf-8') as F1: forLineinchF1:Print(Json.loads (line)) Dict1= {'wo':'Good_person','ni':'Bad_person'}dump and load: Used to write a file in a file F= Open ('Json_file','W', encoding='Utf-8') Json.dump (dict1,f)#Serialization ofF.close () F= Open ('Json_file','R', encoding='Utf-8')Print(Json.load (f))#deserialization#pickle data transfer between Python languages is suitable for almost all data typesImportPickle#dumps loadsDict1 = {'wo':'Good_person','ni':'Bad_person'}ret= Pickle.dumps (Dict1)#bytes TypePrint(ret)Print(Pickle.loads (ret))#deserializationImportPickledict1= {'wo':'Good_person','ni':'Bad_person'}dict2= {'wo':'Good_person','ni':'Bad_person'}dict3= {'wo':'Good_person','ni':'Bad_person'}#Dump Load#Read and write to a file#f = open (' Pickle_files ', ' WB ')#pickle.dump (dict1,f)With Open ('Pickle_files','RB') as F1: whileTrue:Try:            Print(Pickle.load (F1))exceptEoferror: Break

Module-json,pickle

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.