Python Module 1: Python Module
1. Module introduction:
Module, with a lump of code to implement a function of the Code set, similar to functional programming and process-oriented programming, functional programming completes a function, other code can be called, it provides code reusability and code consistency. For a complex function, multiple functions may be required (functions can be different. in The py file), n. the code set composed of py files is called a module;
For example, OS is a system-related module, and file is a file-related module;
There are three modules:
Custom module;
Built-in standard module (also known as standard library );
Open-source module
User-defined modules and open-source modules use reference http://www.cnblogs.com/wupeiqi/articles/4963027.html
(1) Examples of custom modules:
1> module dj example:
2> user_main content:
3> handle file content:
4> SQL _api file content:
5> setting file content:
(2) module calls between different directories:
(3) json & pickle module:
Two modules used for serialization;
1> json: used for conversion between string and python data types;
The json module provides four functions: dumps, dump, loads, and load.
Serialize the dictionary into a string:
Deserializes a string into a dictionary:
2> pickle: used for conversion between python-specific types and python data types;
The pickle module provides four functions: dumps, dump, loads, and load.
Serialize the dictionary:
Deserialization into a dictionary:
3> differences between the two:
Pickle can only be used between python and python, while json can be used between pthon and various languages;
Pickle can serialize functions and so on, while json cannot;
Garbled characters exist after pickle serialization, while strings are serialized after json serialization;
2. Regular Expression basics:
Import re: re module needs to be declared;
Example:
1> match: match from the beginning. If there is no match at the beginning, it will not match;
2> [0-9] indicates only one number, and you need to specify the number of matches again. (match 0 to 10 times)
3> findall: The numbers in the string can be matched;
There is a comma in the middle, and the number of times starts with 0:
There is no comma in the middle, and the number of times starts with 1:
4> match an unconnected string:
There is no comma in the middle, and the number of times starts with 1:
There is a comma in the middle, and the number of times starts with 0:
5> search: Compared with match, search for the numbers that appear in all strings, instead of starting;
6> sub: replace, \ d stands for numbers, \ d + stands for all numbers, and \ d + differs from [0-9:
Replace the first two digits or the first two digits: