Until last one, we made a brief introduction to the knowledge of python modules. Next we introduce the common python modules.
6.3.1 time, datetime module
- use:
- Show time
- Time Conversion
- Time of operation
1. time
import time
time.time ([secs]) Returns the current time timestamp
Timestamp: 1970 to the current second timer unit, the previous time can be specified with a negative
time.gmtime ([secs]) to convert a timestamp truct_time world standard time zone (a tuple, a total of nine elements).
time.localtime ([secs]) to convert a timestamp of the current time zone struct_time. secs parameter is not provided, the current system places on the time
time.struct_time(tm_year=2019, tm_mon=6, tm_mday=27, tm_hour=15, tm_min=0, tm_sec=2, tm_wday=3, tm_yday=178, tm_isdst=0)
time.mktime (t) to convert a timestamp struct_time
time.sleep (secs) thread is suspended specify the time, in seconds
time.asctime ([t])
the tuple indicates a time or struct_time expressed as: 'Sun Oct 1 12:21:11 2018' . If there are no parameters, it will be time.localtime () as a parameter.time.ctime ([secs]) to a converted time stamp: 'Sun Oct 1 12:21:11 2018'. If there are no parameters, it will be time.localtime () as a parameter.
time.strftime (format [, t]) represents a tuple or the time of conversion to struct_time formatted time string. If you do not specify the parameters t, will be time.localtime () as a parameter.
The format can be written references
time.strftime('%Y-%m-%d %H:%M:%S')
time.strptime (string [, format]) represents the time a string the string is converted to struct_time. Reverse corresponds strftime () operation.
time.strftime('%Y-%m-%d %H:%M:%S')
2. datetime
Over time module interface is more intuitive and easier to call
Advantages: Can be time operation
import datetime
datetime.date()
datetime.time()
datetime.datetime()
datetime.timedelta ()
datetime.replace()
Common methods:
import datetime
d = datetime.datetime.now() # 取得当前时间的datetime日期类型数据
d + datetime.timedelta(seconds=10) # 时间的相加,最大单位是天
d.replace(year=2018,month=10) # 替换时间
6.3.2 random module
random.randint (a, b) taking a random number between ab, a and b range comprising
random.randrange (a, b) and the randint () is similar to, but not including the range of b
random.random ()
Random floating pointrandom.choice (str) returns a random character string given in
random.sample (str, n) returns a list of n random characters. It can be used for verification code:
`` `Python
Import Random
Import Strings = string.ascii_lowercase + string.digits + string.punctuation
a = ''.join(random.sample(s,5))
print(a)
```
random.shuffle () shuffle. Upset the order of the list, and re-assigned to the list. Return None
We can also look more random:
`` `Python
Import Random
Import String
s = string.ascii_lowercase + string.digits + string.punctuation
s = list(s)
random.shuffle(s)
s = ''.join(s)
a = ''.join(random.sample(s,6))
print(a)
```