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
bin/: To store some executable files of the project, of course you can name and script/
so on.
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
.
docs/
: Store some documents.
setup.py
: Install, deploy, and package the scripts.
requirements.txt
: A list of external Python packages that store software dependencies.
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:
Select Name,age from staff_table where age > 22
SELECT * from staff_table where dept = "IT"
SELECT * from staff_table where enroll_date like "2013"
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