sys modular

sys The module is with python An interface for interpreter interaction

sys.argv Command line arguments List, The first element is the path of the program itself 
sys.exit(n) Exit procedure , On normal exit exit(0), Wrong exit sys.exit(1)
sys.version obtain Python Explain the version information of the program
sys.path Return the search path of the module , Use... When initializing PYTHONPATH The value of the environment variable
sys.platform Return operating system platform name
import sys
except SystemExit as e:

Exception handling and status


Serialization module

What is serialization —— Put the original dictionary 、 The process of converting a list, etc. into a string is called serialize .

 such as , We are python A data calculated in the code needs to be used by another program , How can we give ?
Now the way we can think of is to put it in a file , And then the other python The program reads it out of the file .
But we all know that , There is no dictionary for files , So we can only convert the data into a dictionary and put it in a file .
You must ask , It's easy to convert a dictionary into a string , Namely str(dic) You can do it , Why should we learn the serialization module ?
Yes, the serialization process is from dic become str(dic) The process of . Now you can go through str(dic), Name one dic The dictionary is converted into a string ,
But how do you convert a string into a dictionary ?
You must have thought of it eval(), If we put a dictionary of string type str_dic Pass to eval, You'll get a returned dictionary type .
eval() Functions are very powerful , however eval What does it do ?e official demo Interpreted as : The string str Evaluate as a valid expression and return the result .
BUT! Powerful functions come at a cost . Security is its biggest weakness .
Imagine , If what we read from the file is not a data structure , It's a sentence " Delete file " Similar destructive statements , So the consequences can't be imagined .
While using eval That's the risk .
therefore , We don't recommend eval Method to deserialize ( take str convert to python Data structure in )

Why have serialization modules

The purpose of serialization

1、 Make custom... In some form of storage Object persistence ;
2、 Passing objects from one place to another .
3、 Make the program more maintainable .


Json The module provides four functions :dumps、dump、loads、load

import json
dic = {'k1':'v1','k2':'v2','k3':'v3'}
str_dic = json.dumps(dic) # serialize : Convert a dictionary to a string
print(type(str_dic),str_dic) #<class 'str'> {"k3": "v3", "k1": "v1", "k2": "v2"}
# Be careful ,json The converted String type of the string in the dictionary is by "" It means dic2 = json.loads(str_dic) # Deserialization : Convert a dictionary in string format to a dictionary
# Be careful , Use json Of loads The string in the dictionary of string type processed by the function must be "" Express
print(type(dic2),dic2) #<class 'dict'> {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} list_dic = [1,['a','b','c'],3,{'k1':'v1','k2':'v2'}]
str_dic = json.dumps(list_dic) # You can also handle nested data types
print(type(str_dic),str_dic) #<class 'str'> [1, ["a", "b", "c"], 3, {"k1": "v1", "k2": "v2"}]
list_dic2 = json.loads(str_dic)
print(type(list_dic2),list_dic2) #<class 'list'> [1, ['a', 'b', 'c'], 3, {'k1': 'v1', 'k2': 'v2'}]

loads and dumps

import json
f = open('json_file','w')
dic = {'k1':'v1','k2':'v2','k3':'v3'}
json.dump(dic,f) #dump Method to receive a file handle , Translate the dictionary directly into json String write file
f.close() f = open('json_file')
dic2 = json.load(f) #load Method to receive a file handle , Put the json String to data structure return

load and dump

import json
f = open('file','w')
json.dump({' nationality ':' China '},f)
ret = json.dumps({' nationality ':' China '})
json.dump({' nationality ':' The United States '},f,ensure_ascii=False)
ret = json.dumps({' nationality ':' The United States '},ensure_ascii=False)

ensure_ascii Key parameters

Serialize obj to a JSON formatted str.( String representation json object )
Skipkeys: The default value is False, If dict Of keys The data in is not python Basic type (str,unicode,int,long,float,bool,None), Set to False when , Will report TypeError Error of . This is set to True, You will skip this kind of key
ensure_ascii:, When it's for True When , All non ASCII The code character is shown as \uXXXX Sequence , Just in dump When will ensure_ascii Set to False that will do , Deposit at this time json Chinese can be displayed normally .)
If check_circular is false, then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).
If allow_nan is false, then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity).
indent: It should be a nonnegative integer , If it is 0 It's the top grid line , If it is empty, it is the most compact display line , Otherwise it will wrap and follow indent The blank line in front of the value display , It's printed out like this json Data is also called pretty-printed json
separators: Separator , It's actually (item_separator, dict_separator) A tuple of , The default is (‘,’,’:’); This means dictionary Inside keys In between “,” separate , and KEY and value In between “:” separate .
default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.
sort_keys: Base the data on keys Sort by .
To use a custom JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise JSONEncoder is used.

Other Parameters

import json
data = {'username':[' Li Hua ',' Er lengzi '],'sex':'male','age':16}
json_dic2 = json.dumps(data,sort_keys=True,indent=2,separators=(',',':'),ensure_ascii=False)

json The formatted output of


json & pickle modular

Two modules for serialization

  • json, For string and python Conversion between data types
  • pickle, be used for python Unique type and python The data types are converted

pickle The module provides four functions :dumps、dump( serialize , save )、loads( Deserialization , read )、load  ( Not only can dictionaries be serialized , list ... You can put python Serialization of any data type in

import pickle
dic = {'k1':'v1','k2':'v2','k3':'v3'}
str_dic = pickle.dumps(dic)
print(str_dic) # A string of binary content dic2 = pickle.loads(str_dic)
print(dic2) # Dictionaries import time
struct_time = time.localtime(1000000000)
f = open('pickle_file','wb')
f.close() f = open('pickle_file','rb')
struct_time2 = pickle.load(f)


At this time, witty you have to say , since pickle So powerful , Why do you have to learn json Well ?
Here we want to explain ,json It's a data structure that all languages recognize .
If we turn a dictionary or sequence into a json It's in the file , that java Code or js Code can also be used .
But if we use pickle serialize , Other languages can't understand what it is ~
therefore , If you serialize a list or dictionary , We highly recommend that you use json modular
But if for some reason you have to serialize other data types , And in the future you will use python To deserialize this data , Then you can use it pickle


shelve It's also python Serialization tools provided to us , Than pickle It's easier to use .
shelve Just give us one open Method , Yes, it is key Visiting , It's similar to a dictionary .

import shelve
f ='shelve_file')
f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'} # Operate directly on file handles , You can store the data
f.close() import shelve
f1 ='shelve_file')
existing = f1['key'] # When taking out the data, you just need to use key Get it , But if key There is no error reporting


There is a limitation to this module , It doesn't support multiple applications going to the same DB Write operation . So when we know that our application only performs read operations , We can get shelve Open as read-only DB

import shelve
f ='shelve_file', flag='r')
existing = f['key']

shelve read-only

because shelve By default, no changes to the object to be persisted are recorded , So we are You need to change the default parameters when you need to , Otherwise, the modification of the object will not be saved .

import shelve
f1 ='shelve_file')
f1['key']['new_value'] = 'this was not here before'
f1.close() f2 ='shelve_file', writeback=True)
f2['key']['new_value'] = 'this was not here before'

Set up writeback

writeback There are advantages and disadvantages in this way . The advantage is that it reduces our chances of making mistakes , And make the persistence of objects more transparent to users ; But this is not always the case , First , Use writeback in the future ,shelf stay open() It will increase the extra memory consumption when it's running , And when the DB stay close() Every object in the cache will be written to DB, It also brings extra waiting time . because shelve There is no way to know which objects in the cache have been modified , Which objects have not been modified , So all objects are written to .

Serializing modules and sys More articles on modules

  1. ( turn )python Common modules ( Explanation of modules and packages ,time modular ,sys modular ,random modular ,os modular ,json and pickle Serialization module )

    Read the directory 1.1.1 The import module 1.1.2__name__ 1.1 modular What is a module : In the development of computer programs , As the program code gets more and more written , The code will get longer and longer in a file , It's getting harder and harder to maintain . To write a maintainable generation ...

  2. python One of the common modules sys modular

    python One of the common modules sys modular 1.sys.argv[]: Command line arguments List, The first element is the program itself # Write a simple python Program , The code is as follows : #!/usr/bin/python #coding= ...

  3. python Common modules ( Explanation of modules and packages ,time modular ,sys modular ,random modular ,os modular ,json and pickle Serialization module )

    1.1 modular What is a module : In the development of computer programs , As the program code gets more and more written , The code will get longer and longer in a file , It's getting harder and harder to maintain . To write maintainable code , We group a lot of functions , Put them in separate files , such , Every article ...

  4. modular —— Serialization module 、random modular 、os modular 、 sys modular 、hashlib modular 、collections modular

    Let's talk about it today Python Module in : Third-party module Can download / install / Use First step : take pip.exe Add the directory to the environment variable : Input pip The third step :pip install The name of the module to install  #pi ...

  5. Python Basics (12)_python Module sys modular 、logging modular 、 serialize json modular 、pickle modular 、shelve modular

    5.sys modular sys.argv Command line arguments List, The first element is the path of the program itself sys.exit(n) Exit procedure , On normal exit exit(0) sys.version obtain Python Explain the version information of the program ...

  6. python Study Day27--time modular 、sys modular 、os Modules and serialization modules

    [ Knowledge point ] 1. Time module : (1) Time stamp time , Greenwich mean time ,float data type Time in London, UK :1970.1.1     0:0:0 Beijing time. :1970.1.1     8:0:0 (2) Structured time , ...

  7. collections modular 、 Time module 、random modular 、os modular 、sys modular 、 Serialization module 、subprocess modular

    One .collections modular 1. Other data types In built-in data type (str.dict.list.tuple.set) On the basis of ,collections The module also provides several additional data types ...

  8. logging modular 、sys modular 、shelve modular

    One .logging modular 1.logging The module is used for logging , A log is a record of a point in time , What happened . 2. Logging is for future review , Extract useful information . 3. How to log : You can open the file directly , Record information , ...

  9. Python journal (logging) modular ,shelve,sys modular

    Rookie Science python Seventeenth days 1.logging modular logging Module is the logging module purpose : Used to keep a log Why keep a log : For future review , Extract useful information How to record files Open file directly , Write in direct ...

Random recommendation

  1. WebApi series ~StringContent And FormUrlEncodedContent

    Back to directory Knowledge point This is a very different article , Less used in projects , But if that happens in the project , We also need to know how to solve , For knowledge StringContent and FormUrlEncodedContent ...

  2. CloudSim4.0 Report errors NoClassDefFoundError,Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.distribution.UniformRealDistribution

    I downloaded it today CloudSim 4.0 Code for , Run the sample program that comes with it , As a result, there are some running errors : The reason is that we can't find org.apache.commons.math3.distribution.UniformReal ...

  3. [Outlook] Reacquire outlook Files that are not accessible in

    Abstract : I got an email from my boss , The email contains jar package , So I can't receive this file ,outlook2010 Middle tip :outlook It's not safe to ban access ……, I believe many people have encountered this problem , I've met... Before , I never thought about solving this problem , ...

  4. Shared Fonts -UI Interface editor (SkinStudio) course

    Add one Label Control , Set font properties Add another Label Control , Font properties are still default Just change the font property to Name Change the field name to... Of the font property you want to use Name Field name is OK ( Such as Label1 Font used )

  5. 【Android - V】 And ViewPager Use

    ViewPager yes Android V4 A control in the package , It is often used as a rolling advertisement on the home page , Also often combined with Fragment To achieve the switching effect of the page . ViewPager and ListView There are many similarities , It's all adapter control ...

  6. -_-#【Backbone】Model

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. UrlEncode code /UrlDecode decode

    public class encode {    public static void main(String[] args) throws UnsupportedEncodingException ...

  8. HTML5 Micro data

    This article is a pure post , The original blogger is doing too much detail Original address : Expand - Micro data - Rich web summary / One . Micro data is ...

  9. Several types of file systems :ext3, s…

    classification :  Architecture design and optimization 1.  ext3 In case of abnormal power failure or system crash ( Dirty power off , unclean system shutdown  ). Each has been mounted ext2 The file system computer must use e2fsck Program to check its consistency ...

  10. Mysql 5.7 Windows edition (zip) It's easy to install

    1. download zip package 2. Find a directory to unzip 3. Simple installation : ...