python基礎之模組part1

來源:互聯網
上載者:User

標籤:常見   hex   oca   mktime   getcwd   小數   第一個   修改   網路編程   

模組:

  模組本質上就是一個Python程式。

  所有說是對象的,一定可以通過  對象.方法  來實現某些操作。

模組種類:

  內建模組

  第三方模組

  自訂模組

import在尋找模組的順序:內建模組---->第三方模組---->自訂模組。

後期學習網路編程(socket)跟線程進程(threading processing)的時候其實就是在學習這些模組。

 

time:

  在Python中時間也是一個類。

時間有三種類型:

  時間戳記:時間戳記表示的是從1970年1月1日00:00:00開始按秒計算的位移量。使用time.time()查看的時間戳記是一個浮點型資料

  結構化時間:元組的形式,共有九個元素

  字串時間:方便人們閱讀的格式。例如:‘2017-06-21 19:31:18‘

樣本:
import timeprint(time.time())print(time.localtime())print(time.strftime("%Y-%m-%d %X"))執行結果:D:\Python\Python36-32\python.exe E:/Python/DAY-12/time模組.py1498045508.7362208time.struct_time(tm_year=2017, tm_mon=6, tm_mday=21, tm_hour=19, tm_min=45, tm_sec=8, tm_wday=2, tm_yday=172, tm_isdst=0)2017-06-21 19:45:08Process finished with exit code 0

三種類型的轉換:

import timeprint(time.localtime(time.time()))    #轉換時間戳記為結構化時間print(time.mktime(time.localtime(time.time())))   #轉換結構化時間為時間戳記print(time.strftime(‘%Y-%m-%d‘,time.localtime(time.time())))  #轉換結構化時間為 字串時間print(time.strptime(‘2017-06-22‘,‘%Y-%m-%d‘))   #轉換字串時間為結構化時間執行結果:D:\Python\Python36-32\python.exe E:/Python/DAY-12/time模組.pytime.struct_time(tm_year=2017, tm_mon=6, tm_mday=21, tm_hour=19, tm_min=50, tm_sec=45, tm_wday=2, tm_yday=172, tm_isdst=0)1498045845.02017-06-21time.struct_time(tm_year=2017, tm_mon=6, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=173, tm_isdst=-1)Process finished with exit code 0

還有一種固定字串結構的轉換:

import timeprint(time.asctime(time.localtime(3212334241)))    #結構化時間轉換為固定格式字串時間print(time.ctime(3213213321))   #時間戳記時間轉換為固定格式字串時間執行結果:D:\Python\Python36-32\python.exe E:/Python/DAY-12/time模組.pySun Oct 18 03:04:01 2071Wed Oct 28 07:15:21 2071Process finished with exit code 0

其他的方法:

import timestart = time.time()time.sleep(5)     #暫停指定的時間後繼續運行,單位為秒stop = time.time()print(‘睡了%s秒‘%(stop-start))執行結果:D:\Python\Python36-32\python.exe E:/Python/DAY-12/time模組.py睡了5.000530958175659秒Process finished with exit code 0

 

random:

  隨機產生模組。  

>>> import random>>> random.random()      # 大於0且小於1之間的小數0.7664338663654585>>> random.randint(1,5)  # 大於等於1且小於等於5之間的整數>>> random.randrange(1,3) # 大於等於1且小於3之間的整數>>> random.choice([1,‘23‘,[4,5]])  # #1或者23或者[4,5]>>> random.sample([1,‘23‘,[4,5]],2) # #列表元素任意2個組合[[4, 5], ‘23‘]>>> random.uniform(1,3) #大於1小於3的小數1.6270147180533838>>> item=[1,3,5,7,9]>>> random.shuffle(item) # 打亂次序>>> item[5, 1, 3, 7, 9]>>> random.shuffle(item)>>> item[5, 9, 7, 1, 3]

一個簡單的隨機驗證碼產生器:

import randomdef valdate_code():    ret = ‘‘    for i in range(5):        num = random.randint(0,9)        a1 = chr(random.randint(97,122))        a2 = chr(random.randint(65,90))        s = random.choice([str(num),a1,a2])        ret = ret+s    return retprint(valdate_code())

 

hashlib:

  hashlib提供了常見的摘要演算法,如MD5,SHA1等。

  摘要演算法又稱雜湊演算法,散列演算法。通過一個函數把任意長度的資料轉換為一個長度固定的資料串。

  摘要演算法是單向的無法復原的。

應用:檔案一致性校正,登入

樣本:計算一段字串的MD5值
import hashlib md5 = hashlib.md5() #可以在這裡的括弧內進行“加鹽處理” 註:加鹽處理就是指在原有的內容上在加上一段字串,提高校正值的複雜性md5.update(‘how to use md5 in python hashlib?‘) #update接收資訊可以重疊接收print md5.hexdigest() #顯示摘要資訊執行結果:d26a53750bc40b38b65a520292f69306

 

os:

  作業系統模組。

  作業系統提供的一些介面,Python來調用。

os.getcwd() 擷取當前工作目錄,即當前python指令碼工作的目錄路徑os.chdir("dirname")  改變當前指令碼工作目錄;相當於shell下cdos.curdir  返回目前的目錄: (‘.‘)os.pardir  擷取目前的目錄的父目錄字元串名:(‘..‘)os.makedirs(‘dirname1/dirname2‘)    可產生多層遞迴目錄os.removedirs(‘dirname1‘)    若目錄為空白,則刪除,並遞迴到上一級目錄,如若也為空白,則刪除,依此類推os.mkdir(‘dirname‘)    產生單級目錄;相當於shell中mkdir dirnameos.rmdir(‘dirname‘)    刪除單級空目錄,若目錄不為空白則無法刪除,報錯;相當於shell中rmdir dirnameos.listdir(‘dirname‘)    列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印os.remove()  刪除一個檔案os.rename("oldname","newname")  重新命名檔案/目錄os.stat(‘path/filename‘)  擷取檔案/目錄資訊os.sep    輸出作業系統特定的路徑分隔字元,win下為"\\",Linux下為"/"os.linesep    輸出當前平台使用的行終止符,win下為"\t\n",Linux下為"\n"os.pathsep    輸出用於分割檔案路徑的字串 win下為;,Linux下為:os.name    輸出字串指示當前使用平台。win->‘nt‘; Linux->‘posix‘os.system("bash command")  運行shell命令,直接顯示os.environ  擷取系統內容變數os.path.abspath(path)  返回path正常化的絕對路徑os.path.split(path)  將path分割成目錄和檔案名稱二元組返回os.path.dirname(path)  返回path的目錄。其實就是os.path.split(path)的第一個元素os.path.basename(path)  返回path最後的檔案名稱。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素os.path.exists(path)  如果path存在,返回True;如果path不存在,返回Falseos.path.isabs(path)  如果path是絕對路徑,返回Trueos.path.isfile(path)  如果path是一個存在的檔案,返回True。否則返回Falseos.path.isdir(path)  如果path是一個存在的目錄,則返回True。否則返回Falseos.path.join(path1[, path2[, ...]])  將多個路徑組合後返回,第一個絕對路徑之前的參數將被忽略os.path.getatime(path)  返回path所指向的檔案或者目錄的最後存取時間os.path.getmtime(path)  返回path所指向的檔案或者目錄的最後修改時間os.path.getsize(path) 返回path的大小

python基礎之模組part1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.