python basic json and pickle

. 1  Import JSON, the pickle
 2  # JSON serialization and deserialization 
. 3 List1 = [. 1, 2,. 3 ]
 . 4  # normally only writing binary file or a character string, other types of data need to be written to the file needs to be serialization; JSON serialization result is the string; JSON results can be used across languages 
. 5 with Open ( " json.txt " , " W " , encoding = " UTF-. 8 " ) aS F:
 . 6      f.write (JSON .dumps (List1))
 . 7  # later want to read the serialized file in which the content needs to deserialize 
. 8 with Open ( " json.txt " , " R & lt " , encoding = " UTF-. 8" ) AS F:
 . 9      json_res = json.loads (reached, f.read ())
 10  Print (type (json_res), json_res)
 . 11  # the pickle serialization and deserialization json list only standard dictionaries serialize objects. , similar to the sequence of the following functions will be given json; pickle should be used and serialization and deserialization, pickle serialized binary result, can only be used in a pickle python language grammar 
12 is  DEF Hello ():
 13 is      Print ( " Hello World " )
 14 dict1 = { " FUNC " :} Hello
 15 with Open ( " pickle.txt " , " WB " ) AS F:
 16      f.write (the pickle.dumps (dict1))# Is equivalent to the pickle.dump (dict1, F) 
. 17 with Open ( " pickle.txt " , " RB " ) AS F:
 18 is      pickle_res = The pickle.loads (reached, f.read ()) # is equivalent to the pickle.load ( f) 
19      Print (of the type (pickle_res), pickle_res)
 20  # recommendations: the object should dump dump and load time and load time

 

Guess you like

Origin www.cnblogs.com/flags-blog/p/11964133.html