七、模块

时间:2019-11-17 18:14:38   收藏:0   阅读:96

1. 序列化模块

1.1 json和pickle的区别

1.2 json模块

1.3 pickle模块

2. 时间模块

2.1 time模块

# time.time(): 获取当前的本地时间戳(从1970年1月1日零点到现在的秒数)
# time.localtime():通过时间戳获取时间元组(默认当前时间)
# time.ctime(): 通过时间戳获取时间字符串(默认当前时间)
# time.strftime(格式化字符串, [时间元组]):通过时间元组格式化时间字符串(时间元组时可选参数)
# time.strptime(时间字符串, 格式化字符串):通过时间字符串提取出时间元组
# time.sleep():程序睡眠等待
# time.perf_counter():用于计算程序运行的时间


import time

result = time.time()
print(1, result, type(result))

result = time.localtime()
print(2, result, type(result))

result = time.ctime()
print(3, result, type(result))

result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(4, result, type(result))

# 注意:两个参数的书写内容必须完全相同
result = time.strptime('2019年11月15日21时41分51秒', '%Y年%m月%d日%H时%M分%S秒')
print(5, result, type(result))

start = time.perf_counter()
time.sleep(5)
end = time.perf_counter()
result = end - start
print(6, result, type(result))

# 输出结结果
1 1573827926.2985218 <class 'float'>
2 time.struct_time(tm_year=2019, tm_mon=11, tm_mday=15, tm_hour=22, tm_min=25, tm_sec=26, tm_wday=4, tm_yday=319, tm_isdst=0) <class 'time.struct_time'>
3 Fri Nov 15 22:25:26 2019 <class 'str'>
4 2019-11-15 22:25:26 <class 'str'>
5 time.struct_time(tm_year=2019, tm_mon=11, tm_mday=15, tm_hour=21, tm_min=41, tm_sec=51, tm_wday=4, tm_yday=319, tm_isdst=-1) <class 'time.struct_time'>
6 5.0000196 <class 'float'>

2.2 datetime模块

# datetime.datetime.now():获取现在本地的时间
# datetime.datetime.utcnow():获取当前的UTC时间
# datetime.datetime.strftime():将datetime.datetime格式的时间转化为指定格式的str
# datetime.datetime.strptime():将str格式的时间转化为datetime.datetime格式的时间
# datetime.timedelta():用作datetime.datetime格式时间的加减运算的参数
# datetime.timezone(datetime.timedelta(hours=n)):获取指定的时区


import datetime

result1 = datetime.datetime.now()
print(1, result1, type(result1))

result2 = datetime.datetime.strftime(result1, '%Y-%m-%d %H:%M:%S')
print(2, result2, type(result2))

result3 = datetime.datetime.strptime(result2, '%Y-%m-%d %H:%M:%S')
print(3, result3, type(result3))

result4 = datetime.datetime.utcnow()
print(4, result4, type(result4))

result5 = datetime.timedelta(days=30, hours=2, minutes=5, seconds=30)
print(5, result5, type(result5))

# 获取与当前UTC时间加上30天2小时5分钟30秒之后的时间
result6 = result4 + result5
print(6, result6, type(result6))

result7 = datetime.timezone(datetime.timedelta(hours=2, minutes=5, seconds=30))
print(7, result7, type(result7))

# 输出结果
1 2019-11-16 11:39:37.096657 <class 'datetime.datetime'>
2 2019-11-16 11:39:37 <class 'str'>
3 2019-11-16 11:39:37 <class 'datetime.datetime'>
4 2019-11-16 03:39:37.120650 <class 'datetime.datetime'>
5 30 days, 2:05:30 <class 'datetime.timedelta'>
6 2019-12-16 05:45:07.120650 <class 'datetime.datetime'>
7 UTC+02:05:30 <class 'datetime.timezone'>

3. random模块

4. shutil模块

# shutil.copy():复制文件的权限和内容
# shutil.move():修改文件名
# shutil.rmtree():删除指定目录下的所有内容
# shutil.make_archive():压缩文件
# shutil.unpack_archive():解压文件

import shutil

shutil.copy('a.txt', 'b.txt')
shutil.move('1.txt', 'c.txt')
path1 = r'C:\Python38\Scripts\exercise\advance\try\a\b'
path2 = r'C:\Python38\Scripts\exercise\advance\try\test'
shutil.make_archive('a_packed', 'zip', path1)
shutil.unpack_archive('b_packed.zip', extract_dir=path2)
shutil.rmtree('a')

5. os模块

6. sys模块

7. logging模块

原文:https://www.cnblogs.com/aaron-zhou/p/11876995.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!