Python 常用模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 常用模块相关的知识,希望对你有一定的参考价值。

time 模块

import time

print(time.time())  # 时间戳
print(time.mktime(time.localtime()))  # struct_time  ——> 时间戳

print(time.strftime(‘%Y-%m-%d %H:%M:%S‘))  # 格式化的字符串
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime()))  # struct_time ——> Format String

print(time.localtime()) # 默认是当前时间
print(time.localtime(1594193367))  # 时间戳 -> struct_time
print(time.gmtime(1594193367))
print(time.strptime(‘2020-07-08 15:45:18‘, ‘%Y-%m-%d %H:%M:%S‘))  # Format String  ——> struct_time

‘‘‘
asctime格式

‘‘‘

print(time.asctime())
print(time.asctime(time.localtime()))
print(time.ctime())
print(time.ctime(time.time()))

datetime 模块

import datetime
## 获取当前时间和日期
now = datetime.datetime.now()
print("=======================================获取当前时间和日期")
print(now)
print(now.strftime(‘%Y-%m-%d %H:%M:%S.%f%z‘))

## 获取指定日期和时间
print("=======================================获取指定日期和时间")

dt = datetime.datetime(2021, 4, 19, 12, 20)
print(dt)
print(dt.strftime(‘%Y-%m-%d %H:%M:%S.%f%z‘))

#datetime转换为timestamp
print("=======================================datetime转换为timestamp")
dt = datetime.datetime(2019, 4, 19, 12, 20)
print(dt.timestamp())

## timestamp转换为datetime
print(‘======================timestamp转换为datetime‘)

print(datetime.datetime.fromtimestamp(1618806000)) # 转换为本地时区
print(datetime.datetime.utcfromtimestamp(1618806000)) #转换为UTC标准时区的时间

## str转为为datatime
print(‘======================str转为为datatime‘)
dt_str = datetime.datetime.strptime(‘2015-6-1 18:19:59.497222+0000‘, ‘%Y-%m-%d %H:%M:%S.%f%z‘)
print(dt_str)

## datetime转换为str
print(‘======================datetime转换为str‘)
print(datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8))).strftime(‘%Y-%m-%d %H:%M:%S.%f%z‘))

## datetime加减
print(‘==================datetime加减‘)
now=datetime.datetime.now();
print(now+datetime.timedelta(hours=2,days=2))
print(now-datetime.timedelta(hours=2,days=2))

## 获取带时区的时间
## datetime表示的时间需要时区信息才能确定一个特定的时间,否则只能视为本地时间。
## 如果要存储datetime,最佳方法是将其转换为timestamp再存储,因为timestamp的值与时区完全无关。

tz_utc_8 = datetime.timezone(datetime.timedelta(hours=8))
tz_utc_0 = datetime.timezone(datetime.timedelta(hours=0))

now1 = datetime.datetime.now(tz=tz_utc_8)
now2 = datetime.datetime.now(tz=tz_utc_0)
print(‘==================获取带时区的时间‘)
print(now1,now2)

## 时区转换
print(‘=================时区转换‘)
## 拿到UTC时区的时间
utc_dt1 = datetime.datetime.now(tz=datetime.timezone.utc)
utc_dt2 = datetime.datetime.now(tz=datetime.timezone(datetime.timedelta(hours=0)))
utc_dt3 = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)

print(‘%s,%s,%s‘ % (utc_dt1, utc_dt2, utc_dt3))

## 将UTC时区转换为北京时区
bj_dt = utc_dt1.astimezone(datetime.timezone(datetime.timedelta(hours=8)))
print(bj_dt)
## astimezone()将转换北京时区为东京时间
tokyo_dt1 = bj_dt.astimezone(datetime.timezone(datetime.timedelta(hours=9)))
## astimezone()将转换UTC时区为东京时间
tokyo_dt2 = utc_dt1.astimezone(datetime.timezone(datetime.timedelta(hours=9)))
print(tokyo_dt1, tokyo_dt2)

print(datetime.datetime.strptime(‘2015-1-21 9:01:30-05:00‘,‘%Y-%m-%d %H:%M:%S%z‘).timestamp())
print(datetime.datetime.strptime(‘2015-6-1 08:10:30+07:00‘,‘%Y-%m-%d %H:%M:%S%z‘).timestamp())

print("UTC+15:00".replace("UTC","").zfill(6))

def to_timestamp(dt_str, tz_str):
    tzinfo=tz_str.replace("UTC","").zfill(6)
    str_date=‘%s%s‘%(dt_str,tzinfo)
    return datetime.datetime.strptime(str_date,‘%Y-%m-%d %H:%M:%S%z‘).timestamp()

## 测试:
t1 = to_timestamp(‘2015-6-1 08:10:30‘, ‘UTC+7:00‘)
assert t1 == 1433121030.0, t1

t2 = to_timestamp(‘2015-5-31 16:10:30‘, ‘UTC-09:00‘)
assert t2 == 1433121030.0, t2

print(‘ok‘)

#  参考 https://www.liaoxuefeng.com/wiki/1016959663602400/1017648783851616

以上是关于Python 常用模块的主要内容,如果未能解决你的问题,请参考以下文章

python常用代码片段总结

nodejs常用代码片段

python常用代码

盘点Python常用的模块和包

Day5 模块及Python常用模块

python常用模块