Python 模版(四)

九、time

時間相關的操作,時間有三種表示方式: 

 時間戳               1970年1月1日之後的秒,即:time.time()

 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')

 結構化時間          元組包含了:年、日、星期等... time.struct_time    即:  time.localtime()

print time.time()
print time.mktime(time.localtime())
  
print time.gmtime()    #可加時間戳參數
print time.localtime() #可加時間戳參數
print time.strptime('2014-11-11', '%Y-%m-%d')
  
print time.strftime('%Y-%m-%d') #默認當前時間
print time.strftime('%Y-%m-%d',time.localtime()) #默認當前時間
print time.asctime()
print time.asctime(time.localtime())
print time.ctime(time.time())
  
import datetime
'''
datetime.date:表示日期的類。常用的屬性有year, month, day
datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond
datetime.datetime:表示日期時間
datetime.timedelta:表示時間間隔,即兩個時間點之間的長度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
'''
import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(days=5)

wKioL1ZmoEvRcTxkAACjhqwpX2c956.png

十、re

re模塊用於對python的正則表達式的操作。

字符:


  . 匹配除換行符以外的任意字符

  \w 匹配字母或數字或下劃線或漢字

  \s 匹配任意的空白符

  \d 匹配數字

  \b 匹配單詞的開始或結束

  ^ 匹配字符串的開始

  $ 匹配字符串的結束

次數:

  * 重複零次或更多次

  + 重複一次或更多次

  ? 重複零次或一次

  {n} 重複n次

  {n,} 重複n次或更多次

  {n,m} 重複n到m次

IP:
^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$
手機號:
^1[3|4|5|8][0-9]\d{8}$

1、match(pattern, string, flags=0)

 從起始位置開始根據模型去字符串中匹配指定內容,匹配單個

    正則表達式

    要匹配的字符串

    標誌位,用於控制正則表達式的匹配方式

import re
obj = re.match('\d+', '123uuasf')
if obj:
    print obj.group()
# flags
I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale
M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments

2、search(pattern, string, flags=0)

根據模型去字符串中匹配指定內容,匹配單個

import re
obj = re.search('\d+', 'u123uu888asf')
if obj:
    print obj.group()

3、group和groups

a = "123abc456"
print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()
print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)
print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)
print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)
print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()

4、findall(pattern, string, flags=0)

上述兩中方式均用於匹配單值,即:只能匹配字符串中的一個,如果想要匹配到字符串中所有符合條件的元素,則需要使用 findall。

import re
obj = re.findall('\d+', 'fa123uu888asf')
print obj

5、sub(pattern, repl, string, count=0, flags=0)

用於替換匹配的字符串

content = "123abc456"
new_content = re.sub('\d+', 'sb', content)
# new_content = re.sub('\d+', 'sb', content, 1)
print new_content

相比於str.replace功能更加強大

6、split(pattern, string, maxsplit=0, flags=0)

根據指定匹配進行分組

content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
new_content = re.split('\*', content)
# new_content = re.split('\*', content, 1)
print new_content
content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
new_content = re.split('[\+\-\*\/]+', content)
# new_content = re.split('\*', content, 1)
print new_content
inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
inpp = re.sub('\s*','',inpp)
new_content = re.split('\(([\+\-\*\/]?\d+[\+\-\*\/]?\d+){1}\)', inpp, 1)
print new_content

十一、random

隨機數

mport random
print random.random()
print random.randint(1,2)
print random.randrange(1,10)

隨機驗證碼實例:

import random
checkcode = ''
for i in range(4):
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print checkcode


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章