package
Package is used to import, also belong to the module.
With __init__
.py file; leader packet is introduced __init__
.py file
package must be imported as a module file, the search path module to perform file path prevail.
Role package
When excess internal function module, in order to facilitate the management module, the module is divided into a plurality of modules, but do not change the way of introduction, put into a module package.
time module
Offers three different types of time, three different types of time can be converted
time.time()
Timestamp
time.sleep(1)
Sleep 1 second
datetime module
You can add and subtract time
now = datetimr.datetime.now() ##计算机当前时间
print(now + datetime.timedelta(3)) ##默认加三天
print(now + datetime.timedelta(weeks=3)) ##加三周
print(now + datetime.timedelta(hours=3)) ##加三小时
print(now - datetime.timedelta(hours=3)) ##减三小时
print(now + datetime.timedelta(hours=-3)) ##减三小时
print(now.replace(year=1949,month=10,day=1,hour=10,minute=1,second=0,microsecond=0)) ##1949-10-01 10:01:00 00(年月日 时分秒 毫秒)
random module
Generate random numbers
grasp
print(random.random()) ##默认0-1的随机数不包括0和1
print(random.random(1,3)) ##随机生成1-3的整数
lt = [1,2,3]
random.shuffle(lt)
print(lt) ##对lt进行乱序
print(random.choice(lt)) ##lt中随机选一个数
To understanding
print(random.sample([1,'a','b',2,3],n))
## randomly selected from the list of n data
hashlib module and the module hmac
hashlib is a character encryption
hmac character is encrypted, and add key
typing module
In conjunction with the function, the function of the control parameter data type, the data type is provided outside of the underlying data type
requests module
Url analog browser sends a request to get data
url: is a specific URL, never repeat
re module
To string, the string looking for certain characteristics in line with
Metacharacters
^
To ... beginning
s = 'abcdab'
res = re.findall('^ab',s)
print(res) ##['ab']找到第一个就不找了
res = re.findall('^bc',s)
print(res) ##[]开头对不上就返回空
$
Ending with ..
s = 'abcdaba'
res = re.findall('ba$',s)
print(res) ##['ba']符合就返回,不符合就返回空
\d
digital
s = 'abc123abc123'
res = re.findall('\d',s)
print(res) ##['1','2','3','1','2','3']只返回数字
\w
Returns a non-empty, numbers, letters, underscores, etc.
s = 'abc 233 dgv 11'
res = re.findall('\w',s)
print(res) ##['a','b',...,'1']除了空格,空等都以单个字符返回
\s
Return to space, \ t, \ n
s = """abc 233
dgv 11"""
res = re.findall('\s',s)
print(res) ##[' ','\n',' ']返回空格,\t,\n
\D
Non-numeric
s = 'abc123abc1_3'
res = re.findall('\D',s)
print(res) ##['a','b',...,'_']返回除数字的字符
+
A character in front of at least one
s = 'abcddd abcd abc'
res = re.findall('abcd+',s)
print(res) ##['abcddd','abcd']
?
In front of the characters 0-1
s = 'abcddd abcd abc'
res = re.findall('abcd?',s)
print(res) ##['abcd','abcd','abc']
*
A character in front of at least 0
s = 'abcddd abcd abc bc'
res = re.findall('abcd*',s)
print(res) ##['abcddd','abcd','abc']
.
Any character
s = 'abc12bca'
res = re.findall('abc.',s)
print(res) ##['abc1']符合.就返回所占位的字符,无或不符合就返回空
* Greedy
. (Any character) * (0- infinite number)
s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbgggg'
res = re.findall('a.*g',s)
print(res) ##['abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbgggg']
. ? Non-greedy mode
. (Any character) (0- infinite number)? (Allowed to enter non-greedy mode)
s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbgggg'
res = re.findall('a.*?g',s)
print(res) ##['abcdefg']
re.S(*****)
Let Matches newline
match
Start from scratch to find, to find not find, can not find on the error
s = 'ab abcddd abc'
res = re.search('abcd*', s)
print(res.group()) ##报错
search
Entire character looking, not looking to find, can not find on the error
s = 'ab abcddd abc'
res = re.search('abcd*', s)
print(res.group()) ##abcddd
Packet
As long as brackets
s = 'abc abcd abcdd'
res = re.findall('a(.)c(d)',s)
print(res) ##[('b','d'),('b','d')]
Famous grouping
As long as brackets to a dictionary
s = 'abc abcd abcdd'
res = re.search('a(?P<name>.)c(?P<name2>d)', s)
print(res.group()) ##{'name':'b','name2':'d'}