Json,pickle Analysis, please advise

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data interchange Format

Because files can only be stored in memory in binary, string format. JSON serialization is required when storing dictionaries and functions. JSON is like a hang in a virtual machine, ready to open and hang at any time.

One, store dictionary data

1. JSON serialized data

Import Jsoninfo = {' age ': $, ' name ': ' Peter ', ' email ': ' @qq. com '}f = open (' Test.txt ', ' W ') #打开test. txt file and write to F.write ( Json.dumps (info)) #json. Dumps encodes the data and is not responsible for writing! Print (Json.dumps (info)) #打印json. Dumps encoded data f.close ()

The data is now stored as a string on the hard disk. If only the data of [age] is removed, the following example is unable to extract the data.

2. Error Demonstration

f = open (' Test.txt ', ' r ') date = F.read () print (date[' age ') f.close ()

Cause analysis: Because the data can only be stored in string, binary form, directly in the form of a dictionary "Date[age" is absolutely impossible to extract.

3. JSON deserialization data

Import jsonf = open (' Test.txt ', ' R ') #打开test. txt file and read date = Json.loads (F.read ()) #json. Loads decode the data print (date[' Age ']) print (date[' name ']) f.close ()

The data decoded by Json.loads will revert to the style of the source data and can be output directly.

4. Another simple method (eval () method)

F.open (' Test.txt ', ' r ') Date = eval (F.read ()) Print (Date ([' Age '])) F.close ()

Second, storage function (pickle)

JSON can only handle simple data types, such as dictionaries, lists, and so on. Unable to process function. json is common in all languages and can be used for interaction between different languages.

Pickle can handle complex data, using exactly the same way as JSON! Pickle applies only to Python

1. Pickle Serialized data

Import pickledef Hello (name): Print (' hello! ', name) info = {' age ': $, ' name ': ' Peter ', ' email ': ' @qq. com ', ' F UNC ': Hello} #函数hello的内存地址传递给func F = open (' test1.py ', ' WB ') #pickle. Dumps () forwards the data directly to binary storage, so you need to use ' WB ' Print (pickle.dump S (info)) #打印pickle编码后的数据 f.write (Pickle.dumps (info)) #写入编码后数据 f.close ()

Analysis: Pickle has a set of grammar rules, the encoded data in the file is in a similar "garbled" look like the storage

2. Pickle Deserialization ( error demonstration )

Import Picklef = open (' Test1.txt ', ' RB ') #存储的二进制, must be read with ' re ' for date = Pickle.loads (F.read ()) print (date)

Cause Analysis: This is not possible at this time. Because a function "hello" is stored in Test1.txt, when the serialized code is executed, the memory address of the function "Hello" is freed, so ' func ' in the dictionary info has no value.

3. Pickle deserialization ( correct demonstration )

Import pickledef Hello (name): Print (' hello,hello!! ', name) F = open (' Test1.txt ', ' RB ') date = Pickle.loads (F.read ()) Print (date[' func ']) #输出内存地址print (date[' func '] (' Peter ') #调用hello函数, output "hello,hello!! , Peter "

Since the function was freed, the function of the same function name was redefined.

Three, small tricks

1. Shorthand method

F.write (Json.dumps (info)) Json.loads (F.read ())

The above code can be abbreviated as:

Json.dump (info,f) json.load (f)

2, multiple dumps, one load (more than 3.0 versions)

Import Jsoninfo = {' age ': $, ' name ': ' Peter ', ' email ': ' @qq. com '}f = open (' Test.txt ', ' W ') Json.dump (info,f) info[' age ']= ' "Json.dump" (info,f) info[' name ']= ' Tom ' Json.dump (info,f)

The result is a three dictionary output, which will go wrong with json.load (f)! But the engraving calls the data through the For loop






Question: 1.pickle when processing a function, whether to serialize the entire function, or the memory address of the function

Json,pickle Analysis, please advise

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.