Pythonで一般的に使用される標準ライブラリの概要

終了し、更新を続けるように努めます

記事ディレクトリ

一.sys

1.オペレーティングシステムを特定します
import sys
print(sys.pltfrom)

Os .os

Three.math

Four.random

ファイブタイム

1.時間関数は、現在のタイムスタンプ(グリニッジ標準時からの合計秒数)を返すために使用されます。
import time
now=time.time()
print(now)

1611303060.773287

2. localtime関数は、タイムスタンプ形式を現地時間に変換して(struct_time)オブジェクトを返すために使用されます。
import time
now=time.localtime()
print(now)

time.struct_time(tm_year = 2021、tm_mon = 1、tm_mday = 22、tm_hour = 16、tm_min = 14、tm_sec = 30、tm_wday = 4、tm_yday = 22、tm_isdst = 0)

3. mktime関数はstruct_timeを受け取り、時間を秒単位で表す浮動小数点数を返します。
import time
now=time.localtime()
print(now)
s=time.mktime(now)
print(s)

time.struct_time(tm_year = 2021、tm_mon = 1、tm_mday = 22、tm_hour = 16、tm_min = 21、tm_sec = 1、tm_wday = 4、tm_yday = 22、tm_isdst = 0)
1611303661.0

4. gmtime関数は、タイムスタンプを0タイムゾーンのstruct_timeに変換できます。
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)

1611303823.960081
time.struct_time(tm_year = 2021、tm_mon = 1、tm_mday = 22、tm_hour = 8、tm_min = 23、tm_sec = 43、tm_wday = 4、tm_yday = 22、tm_isdst = 0)

5. asctime関数は、struct_timeを受け取り、読み取り可能な時間フォームを返すことができます。
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
new=time.asctime(s)
print(new)

1611303959.5288363
time.struct_time(tm_year = 2021、tm_mon = 1、tm_mday = 22、tm_hour = 8、tm_min = 25、tm_sec = 59、tm_wday = 4、tm_yday = 22、tm_isdst = 0)
金1月22日08:25:59 2021

6. ctime関数は、タイムスタンプを読み取り可能な形式に変換できます
import time
now=time.time()
print(now)
s=time.ctime(now)
print(s)

1611304057.0304146
金1月22日16:27:372201

7.スリープ関数は、呼び出し元のスレッドの実行を遅らせます。パラメーターは秒です。
import time
print("start:",time.ctime())
time.sleep(9)
print("end:",time.ctime())

開始:金1月22日16:29:58 2021
終了:金1月22日16:30:072021

8. strftime関数は時間タプルを受け取り、読み取り可能な現地時間を返します
9. strptime関数は、時間文字を時間タプルに解析できます

Six.datetime

1.日付オブジェクト
import datetime
print(datetime.MAXYEAR)   #支持的最大年份
print(datetime.MINYEAR)   #支持的最小年份
print(datetime.date.today())  #today返回当天日期
print(datetime.date.today().weekday())  #weekday放回当天的星期
print(datetime.date.today().isoformat())  #返回IOS格式

9999
1
2021-01-22
4
2021-01-22

2.時間オブジェクト
import datetime
print(datetime.time())	   #默认时间
print(datetime.time.max)   #支持的最大时间
print(datetime.time.min)   #支持的最小时间

00:00:00
23:59:59.999999
00:00:00

3.datetimeオブジェクト

datetime = date + time、日付と時刻の両方を返します

import datetime
today=datetime.datetime.today()
print(today)
print(datetime.datetime.now())   #与today用法相同
print(datetime.datetime.utcnow())  #0时区时间
now=datetime.datetime.now()
print(now.date())   #date对象返回日期
print(now.time())   #time对象返回时间

2021-01-22 18:22:42.380595
2021-01-22 18:22:42.382589
2021-01-22 10:22:42.382589
2021-01-22
18:22:42.382589

4.timedeltaオブジェクト

時差を表します

import datetime
dt1=datetime.datetime.now()
dt2=datetime.timedelta(weeks=2)+dt1
print(dt1)
print(dt2)
print(dt1-dt2)

2021-01-22 18:34:31.429461
2021-02-05 18:34:31.429461-14
日、0 00 00

5.tzinfoオブジェクト

Seven.calendar

1.calendar.isleapは、うるう年を判断するために使用されます
import calendar
print(calendar.isleap(2000))
print(calendar.isleap(2018))


2.canlender.leapdaysは、2年間のうるう年の合計数を返すために使用されます
import calendar
print(calendar.leapdays(1900,2000))

24

3.canlender.monthメソッドを使用してカレンダーを作成します

4つのパラメーター:年、月、w(文字間隔)、l(行間隔)

import calendar
print(calendar.month(2021,1))

ここに写真の説明を挿入

4. calendar.monthcalenderメソッドは、単一レベルのネストされたリストを返し、各リストには1週間が含まれます
import calendar
print(calendar.monthcalendar(2021,1))

[[0、0、0、0、1、2、3]、[4、5、6、7、8、9、10]、[11、12、13、14、15、16、17]、[ 18、19、20、21、22、23、24]、[25、26、27、28、29、30、31]]

5. calendar.monthrangeメソッドは、2つの整数のタプルを返します。最初の数値は月の最初の日の曜日を示し、2番目の数値は月を変更する日数を示します。
import calendar
print(calendar.monthrange(2021,1))

(4、31)

6.calendar.weekdayメソッドは、指定された日付の週コードを返します
import calendar 
print(calendar.weekday(2021,1,1))

4

7.calendar.calendarは、年間カレンダー全体を返します
import calendar 
print(calendar.calendar(2021))

ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/qq_50216270/article/details/112985706