论Python常见内置模块

Python常见内置模块

sys模块:

sys.argv() # 在Python脚本传参使用(很重要)
sys.exit() # 系统退出
sys.getdefaultencoding() # 获取系统默认编码getfilesystemencoding() # 获取文件编码
getrecursionlimit() # 获取系统默认递归的最大层数
setrecursionlimit(num) # 设置递归的最大层数
getrefcount() # 获取对象的引用计数的数量

hashlib模块:

加密,散列加密(hash(哈希)加密)

根据加密是否可逆可分为:
可逆加密

根据加密和解密的秘钥是否是同一个可分为:
①对称加密
DES
②非对称加密
RSA

不可逆加密

hash是典型的不可逆加密
MD5、shal256
import hashlib
MD5码:

md5 = hashlib.md5("需要加密的数据".encode("utf-8"))

base64模块

encode方法

描述
Python encode() 方法以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。

语法
encode()方法语法:

str.encode(encoding='UTF-8',errors='strict')

参数
encoding – 要使用的编码,如"UTF-8"。
errors – 设置不同错误的处理方案。默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。

返回值
该方法返回编码后的字符串。

decode方法

描述
Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。

语法
decode()方法语法:

str.decode(encoding='UTF-8',errors='strict')

参数
encoding – 要使用的编码,如"UTF-8"。

errors – 设置不同错误的处理方案。默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。

返回值
该方法返回解码后的字符串。

time模块:

asctime() # 获取系统当前时间
ctime() # 获取系统当前时间
time() # 获取当前的时间戳
localtime() # 返回当前时间,以类似于元组的对象

t = time.localtime()
		print("当前时间是%s-%s-%s %s:%s:%s" %(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec))

time.strftime() # 将时间对象格式化成字符串

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

time.strptime() # 时间字符串转换为时间对象

time.strptime('2019/09/18 21:02:44', "%Y/%m/%d %H:%M:%S")

Calendar模块

实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import calendar
 
cal = calendar.month(2016, 1)
print "以下输出2016年1月份的日历:"
print cal
以上实例输出结果:

以下输出2016年1月份的日历:
    January 2016
Mo Tu We Th Fr Sa Su
             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

datetime

`datetime.datetime.now()`	# 获取系统当前时间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SLASH-YONG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值