collections module, time & datetime module .os & sys module, The subprocess module, Random module, serialization module (json, pickle)

collections module, time & datetime module .os & sys module, The subprocess module, Random module, serialization module (json, pickle)

A, Collections module

On the basis of the basic data types (int, float, list, tuple, dict, set, files) on, collections module also provides other additional data types

Counter: a counter for counting

Deque: deque, from the other end side can quickly and additional objects Release

Defaultdict: Dictionary with default values

Ordereddict: ordered dictionary

Namedtuple: generating tuple can be accessed by name

1.namedtuple generated tuple can be accessed by name

2.deque deque

Mentioned here queue (queue), the queue is a first in first out (FIFO first in first out)

3.Orderddict: ordered dictionary

It should be noted:

When using the Dictionary, key value disorderly. When dict iterate again, we can not determine the order of the key.

Python3 indistinguishable in the drawings, is able to distinguish the python2 out.

If you want to keep the key sequence, you can use orderddict

4.defaultddict have default values ​​dictionary

When using dict, if the referenced key is not present, it will be thrown keyerror. If you want the key does not exist, a default value is returned, you can use defaultdict

 

5.counter a counter for counting, in the form of a dictionary is returned

Two, ime module

Three forms of time

1. timestamp

2. Format of the time string (used to display posters have)

3. Structured time

 

Timestamp (timestamp)

Time.time()

Formatted time string (format string)

( '% Y-% m-% d')) ( '% Y-% m-% d% H:% M:% S')) ( '% Y-% m-% d% X')) # % X 等价 于% H:% M:% S

Structured time (struct_time)

Time.struct_time()

Time.localtime(sec)   表示1970年1月1日开始,经历的时间 转为结构化格式 不写sec,默认当前时间的结构化格式

Time.sleep(sec)

Datetime 模块

三、Random 模块

Os模块and sys模块

Os模块:它是来跟操作系统打交道的

Sys模块:它是跟python解释器打交道的

四、序列化模块

序列:字符串

序列化:其他数据类型转换成字符串的过程

反序列化:将字符串转成其他数据

写入文件的数据必须是字符串

基于网络传输的数据必须是二进制

 

序列化中用到的两个模块:json  pickle

1.Json模块:

 Json模块提供了四个功能:dumps, dump,loads,load

Json模块 中ensure_ascii方法 (字符串中有中文需要注意)

2.Pickle 模块

注意:

Json 是用于字符串和python数据类型间进行转换

Pickle 是用python特有的类型 和python的数据类型间进行转换  用pickle操作文件的时候 文件的打开模式必须是b模式

pickle模块提供了四个功能:dumps、dump(序列化,存)、loads(反序列化,读)、load。(不仅可以序列化字典,列表...可以把python中任意的数据类型序列化

Subprocess 模块

Sub :子

Process:进程

 

1.用户通过网络连接上你的电脑

2.用户输入相应的命令 基于网络发送给了你电脑上的某个程序

3.获取用户命令  利用subprocess 执行该用户命令

4.将执行结果再基于网络发送给用户

这样就实现  用户远程操作你这台电脑的操作

 

Guess you like

Origin www.cnblogs.com/oldboyliuhong/p/11210174.html