python 3.x 學習筆記7 ( 模組 ),python3.x

來源:互聯網
上載者:User

python 3.x 學習筆記7 ( 模組 ),python3.x

1.定義:
模組:用來從邏輯上組織python代碼(變數、函數、類、邏輯:實現一個功能),本質就是.py結尾的python檔案
包:用來從邏輯上組織模組的,本質就是一個目錄(必須帶有一個__init__.py檔案)

2.匯入方法
import module_name1,module_name2

from module import * (不建議使用)

from module import m1,m2,m3
from module import m1 as m

3.import 本質
匯入模組的本質就是把python檔案解釋一遍
匯入包的本質就是執行該包下面的__init__.py檔案

4.匯入最佳化
from module_test import test

5.模組分類
  a.標準庫
  b.開源模組
  c.自訂模組

標準庫:
1.time與datetime

strftime("格式",struct_time)----->"格式化的字串"
strptime("格式化的字串","格式")

6.模組大全

http://blog.51cto.com/egon09/p2

 

7.例子

import timeprint(time.time())                      #返回目前時間的時間戳記print(time.altzone)                     #返回與utc時間的時間差,以秒計算  -32400print(time.asctime())                   #返回時間格式"Fri Jan 26 20:38:48 2018",print(time.gmtime())                     #gmtime()方法是將一個時間戳記轉換為UTC時區(0時區)的struct_time
print(time.mktime(time.localtime())) #將一個struct_time轉化為時間戳記
print(time.asctime()) #把一個表示時間的元組或者struct_time表示為這種形式:'Fri Jan 26 20:38:48 2018'。如果沒有參數,將會將time.localtime()作為參數傳入。

print(time.asctime(time.gmtime())) # 把一個表示時間的元組或者struct_time表示為這種形式:'Fri Jan 26 20:38:48 2018'。如果沒有參數,將會將time.localtime()作為參數傳入。


print(time.ctime()) #把一個時間戳記(按秒計算的浮點數)轉化為time.asctime()的形式。如果參數未給或者為None的時候,將會預設time.time()為參數。它的作用相當於time.asctime(time.localtime(secs))。

string_2_struct = time.strptime("2018-01-26 20:38:48","%Y-%m-%d %H:%M:%S") #將 日期文字 轉成 struct時間對象格式print(string_2_struct)print(time.gmtime(time.time() - 98546) # 將utc時間戳記轉換成struct_time格式print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #將utc struct_time格式轉成指定的字串格式import datetimeprint(datetime.datetime.now())print(datetime.date.fromtimestamp(time.time())) # 時間戳記直接轉成日期格式2018-01-26print(datetime.datetime.now()+datetime.timedelta(6)) #目前時間+6天print(datetime.datetime.now()-datetime.timedelta(6)) #目前時間-6天print(datetime.datetime.now()+datetime.timedelta(hours=6)) #目前時間+6小時print(datetime.datetime.now()+datetime.timedelta(minutes=30)) #目前時間+30分c_time = datetime.datetime.now()print(c_time.replace(minute=3,hour=2)) #時間替換

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.