Python Automation Development Learning 4-3

Source: Internet
Author: User

JSON and Pickle

serialization : Turns the data object into a string, which can be saved in a file. The reverse is the inverse of the serialization

Python's own str () can be serialized, and Eval () can be deserialized, but let's forget about them first. Do not know the scope of application is how big.

We use the JSON specification for serialization and deserialization.

Import Jsondica = {' name ': ' Alice ', ' age ': +, ' from ': ' shanghai '}stra = Json.dumps (dica) # serialized print (t Ype (Stra), stra) DICA2 = Json.loads (stra) # Deserialize print (Type (DICA2), DICA2) strb = ' {' name ': ' Jack ', ' age ': ' From ': ' Beijing '} ' # JSON only double quotation marks, so the inside of the string to double quotation marks print (type (STRB), strb) DICB = Json.loads (STRB) # Deserialize print (Type (DICB), DICB)

One requirement is to serialize into a string and save the file after it is stored. The next time you need to read the file, deserialize the data before it is generated. For this case, there are two convenient ways to do this directly.

Import Jsondica = {' name ': ' Alice ', ' age ': +, ' from ': ' Shanghai '}with Open ("Testjson.txt", ' W ', encoding = ' Utf-8 ') as File:json.dump (Dica,file)

You can check to see if a file is generated in the running directory. And then deserialize it again.

Import Jsonwith Open ("Testjson.txt", ' R ', encoding= ' Utf-8 ') as File:data = json.load (file) print (type (data), data)

The serialization of the above JSON does not support all Python data types. But JSON is a common specification, which is that JSON-serialized data can be recognized in other locales as well.

For unsupported data types, it should be possible to add one-step codec, but if the other locale does not support this data type, it will not work even if it is serializable.

However, after Python serialization is saved and then deserialized for Python, there is no problem with the data type, so you can use pickle.

Python's pickle module implements all data sequences and deserialization of Python. There is basically no big difference between functional use and JSON modules, and the same approach is dumps/dump and loads/load

Import Pickledica = {' name ': ' Alice ', ' age ': +, ' from ': ' shanghai '}stra = Pickle.dumps (dica) # serialization PRI NT (Type (stra), stra) # Here bytes type, only binary type is possible to serialize all data types DICA2 = Pickle.loads (stra) # Deserialize print (Type (DICA2), DICA2)

Software Catalog Development Specification

Suppose the project name is foo and the first letter of the project name is capitalized. The following is a simple directory structure:

foo/
|--bin/
| |--Foo
|
|--foo/
| |--tests/
|   | |--__init__.py
|   | |--test_main.py
| |
| |--__init__.py
| |--main.py
|
|--docs/
| |--conf.py
| |--Abc.rst
|
|--setup.py
|--Requirements.txt
|--README


    1. bin/: To store some executable files of the project, of course you can name and script/ so on.

    2. foo/: stores all source code for the project. (1) All modules and packages in the source code should be placed in this directory. Do not place the top level directory. (2) Its subdirectory tests/ holds unit test code, and (3) The entrance of the program is preferably named main.py .

    3. docs/: Store some documents.

    4. setup.py: Install, deploy, and package the scripts.

    5. requirements.txt: A list of external Python packages that store software dependencies.

    6. README: Project description file.

Module calls between different directories

Python in a folder if there is a __init__.py empty file, this is not an ordinary folder, this is a package.

To invoke the module in the package, you can use: the From Package name Import module name

To be able to invoke, you must first ensure that the parent directory of the package is in the environment variable. To dynamically get to the program's directory and parent directory, and import environment variables, see the following example:

Import Os,sysprint (__file__) # Print relative path print (Os.path.abspath (__file__)) # Print absolute path print (Os.path.dirname (os.path.abspath  (__file__))) # Print the top level directory, here is remove the file name print (Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) # Print and then the first level of the directory Base_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)) # As long as this sentence, do not print (Sys.path) # Printing the current environment variable Sys.path.append (base_dir) # Add environment variable, this sentence also wants. Print (Sys.path) # See if there is an increase in the current environment variable

In practical use, we only need the above 2 sentences on it.

Homework

Employee Information Sheet:

staff_id
Name
Age
Phone
Dept
Enroll_date
1
Adam Liu
25
13562984561
IT
2013-04-01
2
Barry Allen
22
13659874522
HR
2015-05-03
3
Clark Kent
30
13156998456
Sales
2016-04-22
4
Eddie Thawne
40
13566942130
HR
2009-03-01

However, this table may be present in your file:

1,adam liu,25,13562984561,it,2013-04-01

Now need to this employee information file, to achieve additions and deletions to change the operation

One, can be fuzzy query, syntax supports at least the following 3 kinds:

    1. Select Name,age from staff_table where age > 22

    2. SELECT * from staff_table where dept = "IT"

    3. SELECT * from staff_table where enroll_date like "2013"

    4. Find the information, after printing, the last side to show the number of found

Second, can create new employee record, with phone to do unique key, staff_id need to increase

Third, can delete the designated employee information record, enter the employee ID, you can delete

Four, can modify employee information, the syntax is as follows:

Update staff_table set dept= "Market" where dept = "IT"

Note: The above content should be fully used to minimize duplication of code.

Python Automation Development Learning 4-3

Related Article

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.