1. 程式人生 > >python中常用的模組

python中常用的模組

由於是在自己以前的筆記上貼上而來的,所以格式和樣式都不太好看。先湊合看吧,有時間再整理 以下進入正題:

time

  • sleep:休眠指定的秒數(可以是小數)
  • time:獲取時間戳(從1970-01-01 00:00:00到此刻的秒數)
  • localtime:將一個時間戳轉換為一個物件,物件中包含了所有日期時間資訊 # 將時間戳轉換為一個物件(包含年月日等資訊) # 當不傳引數,預設轉換當前時間戳 local_time = time.localtime() print(local_time) # 通過下表獲取 print(local_time[0]) # 通過屬性名獲取 print(local_time.tm_year)
  • mktime:將日期時間資訊轉換為時間戳 # 將日期時間資訊轉換為時間戳 # new_time = time.mktime(local_time) new_time = time.mktime((2018, 6, 20, 10, 32, 10, 2, 171, 0)) print(new_time)
  • strftime:將原則物件格式化顯示 # 格式化顯示,可以傳遞一個類似於元組的物件 print(time.strftime(’%Y-%m-%d %H:%M:%S %w %W’, local_time)) # 不傳時間物件,預設轉換當前時間 print(time.strftime(’%D’)) ‘’’ %Y:年(4位) %y:年(2位) %m:月 %d:日 %D:月/日/年 %H:時(24) %I:時(12) %M:分 %S:秒 %w:星期(1~7) %W:本週是今年的第幾周 ‘’’
  • gmtime:將時間戳轉換為localtime物件 # 將時間戳轉換為localtime物件 print(time.gmtime(time.time())) # 不傳引數預設轉換當前時間戳 print(time.gmtime())
  • asctime:將localtime以特定的格式顯示 # 將localtime格式化顯示 print(time.asctime(local_time)) # 不傳引數預設轉換當前時間 print(time.asctime())
  • timezone:時區 # 返回0時區與當前時區相差的秒數 print(time.timezone)

calendar

  • 使用 import calendar

    # 返回某一年的日曆
    c = calendar.calendar(2018, w=3, l=2, c=10, m=2)
    # print(c)
    
    # 返回某一年某一月的日曆
    m = calendar.month(2018, 6, w=3, l=2)
    # print(m)
    
    # 判斷一個年份是否是閏年
    print(calendar.isleap(2008))
    
    # 返回兩個年份之間的閏年數 [起始年,結束年)
    print(calendar.leapdays(2000, 2008))    # 返回2
    print(calendar.leapdays(2000, 2009))    # 返回3
    

datetime

  • date from datetime import date import time

    # 建立物件
    d1 = date(2018, 6, 20)
    print(d1)
    
    d2 = date.today()
    print(d2)
    
    d3 = date.fromtimestamp(time.time())
    print(d3)
    
    # 轉換為標準格式字串
    s = d1.isoformat()
    print(s)
    # print(type(s))
    
    # 日曆顯示:(年,第幾周,星期)
    print(d1.isocalendar())
    
    # 獲取星期(1~7)
    print(d1.isoweekday())
    # 獲取星期(0~6)
    print(d1.weekday())
    
    # 格式化
    print(d1.strftime('%Y/%m/%d'))
    
    # 轉換為類似於元組的形式(localtime)
    print(d1.timetuple())
    
  • time from datetime import time

    # 建立物件
    t = time(1, 2, 3)
    print(t)
    
    # 單獨獲取
    print('時', t.hour)
    print('分', t.minute)
    print('秒', t.second)
    
    # 格式化顯示
    print(t.strftime('%H::%M::%S'))
    
  • datetime from datetime import datetime import time

    # 建立物件
    dt = datetime(2018, 6, 20, 14, 29, 30)
    print(dt)
    # 當前時間,當地帶時區的時間
    dt2 = datetime.now()
    print(dt2)
    # 不帶時區的時間
    dt3 = datetime.utcnow()
    print(dt3)
    # 將時間戳轉換為日期時間
    dt4 = datetime.fromtimestamp(time.time())
    print(dt4)
    
    # 提取日期
    d = dt.date()
    print(d)
    # print(type(d))
    # 提取時間
    print(dt.time())
    # 轉換為時間戳
    print(dt.timestamp())
    
    # 格式化顯示
    print(dt.strftime('%Y/%m/%d %H:%M:%S'))
    
  • timedelta from datetime import datetime, timedelta

    d1 = datetime(2018, 6, 19, 10, 11, 12)
    d2 = datetime(2018, 6, 20, 12, 14, 18)
    
    # 得到時間差物件timedelta
    delta = d2 - d1
    print(delta)
    print(type(delta))
    
    delta2 = timedelta(days=2, hours=3, minutes=4, seconds=6)
    print(delta2)
    
    d3 = d1 + delta2
    print(d3)
    
    # 天數
    print(delta2.days)
    # 除天以外的秒數
    print(delta2.seconds)
    # 總共的秒數
    print(delta2.total_seconds())