python--regular expression, re module, Collections module, Random module, time module

Source: Internet
Author: User
Tags format definition

Regular expressions
Meta-character quantifier (?)
Metacharacters:
.
\w \d \s \w \d \s
\ t \ \b
() |
[] [^]
^ $
Quantifiers:
* + ?
{n}, {n,}, {n,m}

Re module
How to invoke the RE module
Find: FindAll search match Finditer
Split and replace: Split Sub Subn
Compile: Compile Save time
How many places is the RE module used in?
Quantifier: Indicates a match 0 or 1 times
After the quantifier: is the sign of the lazy match
Group naming: (? p<name> Regular expression) when the reference (? P=name)
Findall/split Ungroup Priority: (?: Regular expression)
Collections Module
How to call the collections module
Namedtuple can name a tuple
Ordereddict ordered Dictionary
Defaultdict Default Dictionary
Deque Dual-ended queue
Counter counter
Queue queues
Advanced first-out features


RANDOMD module:
#随机小数 (Random,uniform), #随机整数 (Randent,randrange) #随机选择一个返回 (choice) #随机选择多个返回, the number returned is the second parameter of the function (sample)
#打乱列表顺序 (Shuffle)
Application scenario: On the phone, the page verification code
# 6-bit/4-digit number--Verification code
Import Random
s = '
For I in range (6):
num = str (random.randint (0,9))
s + = num
Print (s)

6-digit + letter-verification Code
CHR--Characters
res = '
For I in range (6):
num = str (random.randint (0, 9)) #数字
Al_num = Random.randint (97,122) #随机小写字母
al_num2 = Random.randint (65,90) Random capital letters
Alpha = Chr (al_num)
ALPHA2 = Chr (al_num2)
Res + = Random.choice ([NUM,ALPHA,ALPHA2])
Print (RES)
#生成随机验证码
Import Random
Def v_code ():
Code = "
For I in range (5):
Num=random.randint (0,9)
ALF=CHR (Random.randint (65,90))
Add=random.choice ([num,alf])
Code= "". Join ([Code,str (add)])
Return code
Print (V_code ())


#时间模块
Import Time #导入时间模块
#时间戳 (floating-point time):
Time.time ()
Print (Time.time ())
Print (Time.sleep (1))
#格式化时间 Time string
Time.strftime ("%y-%m-%d%x")


#格式化时间-----String for people to see.
Print (Time.strftime ('%h:%m:%s '))
Print (Time.strftime ('%y/%m/%d '))
Print (Time.strftime ('%y-%m-%d '))
Print (Time.strftime ('%c '))


Print (Time.strftime ('%c))

#结构化时间 (tuple)
Print (Time.locatime ())
# # (2018, 5, 22, 10, 36, 31, 1, 142, 0)

String time <--> structured time <---> timestamp time

Timestamps-transitions between structs
Print (Time.time ())
Print (Time.localtime (1500000000))
Print (Time.localtime (1600000000))
Print (Time.localtime (2000000000))
Print (Time.localtime (3000000000))
Print (Time.gmtime (3000000000))

Struct_time = Time.localtime (3000000000)
# Print (Time.mktime (struct_time))
ret = time.strftime ('%y/%m/%d%H :%m:%s ', struct_time)
Print (ret)
s_t = time.strptime (' 2028-5-21 ', '%y-%m-%d ')
Print (s_t)
Print ( Time.mktime (s_t))
#结构化时间 time stamp
#time. Mktime (structured time)
Time.mktime (time_tuple)
#结构化时间--string time
#time. strftime ("format definition", "structured Time") if the structured time parameter does not pass, the actual current time
Time.strftime ("%y-%m-%d%x"

#字符串时间-to-structured time
#time. Strptime (Time string, string corresponding format
Time.strptime ("2017-03-16", "%y-%m-%d")
Time difference calculation:
1. Convert to a floating-point timestamp 2. Two timestamp subtraction, 3. In turn back
Import time
True_time=time.mktime (Time.strptime (' 2017-09-11 08:30:00 ', '%y-%m-%d%h:%m:%s '))
Time_now=time.mktime (Time.strptime (' 2017-09-12 11:00:00 ', '%y-%m-%d%h:%m:%s '))
Dif_time=time_now-true_time
Struct_time=time.gmtime (Dif_time)
Print ('%d '%d '%d '%d '%d '% (struct_time.tm_year-1970,struct_time.tm_mon-1,
Struct_time.tm_mday-1,struct_time.tm_hour,
STRUCT_TIME.TM_MIN,STRUCT_TIME.TM_SEC))

python--regular expression, re module, Collections module, Random module, time module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.