Python basic json and pickle, jsonpickle
Json and pickle are serialized data formats. Before learning json and pickle, we had access to the eval function. Why is this function used? In fact, this function is used to extract the data type in the string. When I first learned the song method, I felt awesome, awesome, but, and eval methods have limitations. for common data types, json. both loads and eval can be used, but eval does not work in case of special types. Therefore, eval is usually used to execute a string expression and return the value of the expression.
# --- Conversion type d = {"name": "yuan"} s = str (d) print (type (s) d2 = eval (s) print (d2 [1]) with open ("test") as f: for I in f: if type (eval (I. strip () = dict: print (eval (I. strip () [1])
View Code
The process of changing objects (variables) from memory to stored or transmitted is called serialization. in Python, it is called pickling. In other languages, it is also called serialization, grouping alling, flattening, etc, all mean. After serialization, The serialized content can be written to the disk or transmitted to another machine over the network. In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling.
Now let's introduce the awesome json:
If we want to pass objects between different programming languages, we must serialize objects into standard formats, such as XML, but a better way is to serialize them to JSON, JSON is a string that can be read by all languages, stored on disks or transmitted over the network. JSON is not only a standard format, but also faster than XML, and can be read directly on the Web page, which is very convenient.
In the past few days, data is stored and extracted from text files and operated using json-type "strings". Many errors have occurred during this process, let's make a summary of the pitfalls I skipped today!
Let's take a look at the usage of json in text files. The key is to learn this,
# -------------------------- Serialize import jsondic = {'name': 'alvin ', 'age': 23, 'sex': 'male'} print (type (dic )) # <class 'dict '> data = json. dumps (dic) print ("type", type (data) # <class 'str'> print ("data", data) f = open ('serialization object ', 'W') f. write (data) # ------------------- is equivalent to json. dump (dic, f) f. close () # ----------------------------- deserialization <br> import jsonf = open ('serialization object') new_data = json. loads (f. read () # equivalent to data = json. load (f) print (type (new_data ))
It seems that the operation is very simple. That's right, he will give you this illusion, and then you can perform it yourself and then drop it! It looks like there's no problem, right? Well, I'll ask you a question.
def oo(): with open('new_hello','r') as f: #for i in f: data = json.loads(f.read()) # print(data) # ret.append(data) return datares = oo()print(res)
As a result, you will find: Yes, this is the error. It has tested my entire day. Why does it come up with many reasons, let me talk about what I met,
1. from the code above, you will see that I load the content of the file into the memory at a time, and then print the loads. Of course, this will report an error. json is a row of data, if you perform this operation, the next line will overwrite the previous line. This will definitely cause problems! So, you don't need to talk about how to do it. You can only traverse the output cyclically. This is one of the solutions to this problem,
2. You cannot leave blank lines during file storage. If you manually store dictionary-type data in the file, you must use double quotation marks. Remember this !!!
Notes for using json:
Import json # dct = "{'1': 111}" # json does not recognize single quotes # dct = str ({"1": 111}) # error, because the generated data is still single quotes: {'one': 1} dct = '{"1": "111"}' print (json. loads (dct) # conclusion: # No matter how data is created, json can be used as long as the data meets the json format. when loads comes out, it doesn't have to be dumps data for loads.
Pickle
Import pickle dic = {'name': 'alvin ', 'age': 23, 'sex': 'male'} print (type (dic )) # <class 'dict '> j = pickle. dumps (dic) print (type (j) # <class 'bytes '> f = open ('serialized object _ pickle', 'wb ') # note that w is written to str, wb is written to bytes, and j is 'bytes 'f. write (j) # ------------------- is equivalent to pickle. dump (dic, f) f. close () # ------------------------- deserialization import picklef = open ('serialized object _ pickle', 'rb') data = pickle. loads (f. read () # equivalent to data = pickle. load (f) print (data ['age'])
The Pickle problem is the same as the serialization problem exclusive to all other programming languages, that is, it can only be used in Python, and different versions of Python are not compatible with each other. Therefore, only Pickle can be used to save unimportant data. It does not matter if deserialization fails.
Finally, we will summarize json as follows:
Json Description: Json, full name JavaScript Object Notation, is a lightweight data exchange format. Json is the most widely used data format for communication between web servers and clients in AJAX.
The Encode process is a process of converting a python object into a json object. The two commonly used functions are dumps and dump. The only difference between the two functions is that dump converts a python object to a json object to generate a fp file stream, while dumps generates a string:
The Decode process is a process of converting a json object into a python object. Two common functions are loads and load functions. The difference is the same as that between dump and dumps.