Python3.5内置模块详解:shelve、xml、configparser、hashlib与hmac
PDF格式 | 101KB |
更新于2024-09-02
| 112 浏览量 | 举报
本文主要探讨了Python3.5中的五个内置模块:shelve、xml、configparser、hashlib以及hmac,并提供了相应的实例解析它们的功能和使用方式。
1、shelve模块
shelve模块提供了一个简单的键值对存储机制,用于持久化Python对象。它通过pickle模块将数据序列化,使得在存储和读取时能保持对象的原始状态。下面是一个使用shelve模块的例子:
```python
import shelve
import datetime
# 打开一个shelve文件
d = shelve.open('shelve_test')
# 存储不同类型的数据
info = {"age": 23, "job": "IT"}
name = ["alex", "rain", "test"]
d["name"] = name # 存储列表
d["info"] = info # 存储字典
d["data"] = datetime.datetime.now()
# 关闭shelve文件
d.close()
```
读取shelve中的数据,可以通过get方法完成:
```python
import shelve
import datetime
# 打开shelve文件
d = shelve.open('shelve_test')
# 读取存储的数据
print(d.get("name")) # 输出列表
print(d.get("info")) # 输出字典
print(d.get("data")) # 输出日期时间对象
# 关闭shelve文件
d.close()
```
2、xml模块
Python的xml模块用于处理XML文档,包括解析XML文档、创建XML文档、操作XML元素树等。例如,可以使用ElementTree API来解析XML文件:
```python
import xml.etree.ElementTree as ET
# 解析XML文件
tree = ET.parse('example.xml')
root = tree.getroot()
# 遍历XML元素
for child in root:
print(child.tag, child.attrib)
```
3、configparser模块
configparser模块用于读写INI格式配置文件,常用于保存应用的设置。下面是一个使用configparser的例子:
```python
import configparser
# 创建配置文件
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45'}
config['COMPUTER1'] = {'Host': 'alpha', 'IP': '192.168.1.1'}
config['COMPUTER2'] = {'Host': 'beta', 'IP': '192.168.1.2'}
# 将配置写入文件
with open('config.ini', 'w') as configfile:
config.write(configfile)
# 读取配置文件
config = configparser.ConfigParser()
config.read('config.ini')
# 输出配置信息
print(config['COMPUTER1']['Host'])
print(config['COMPUTER1']['IP'])
```
4、hashlib模块
hashlib模块提供了各种哈希函数,如MD5、SHA1、SHA256等,用于计算字符串的哈希值。例如,计算一个字符串的SHA256哈希:
```python
import hashlib
message = 'Hello, World!'
hash_object = hashlib.sha256(message.encode())
hex_dig = hash_object.hexdigest()
print(hex_dig)
```
5、hmac模块
hmac模块用于生成消息认证码(HMAC),它结合了密钥和哈希函数,用于验证数据的完整性和来源。以下是如何使用hmac模块生成HMAC的示例:
```python
import hmac
import hashlib
key = b'secret key'
message = b'Hello, HMAC!'
hmac_digest = hmac.new(key, message, hashlib.sha256).digest()
print(hmac_digest)
```
以上五个模块是Python3.5中常用的内置模块,它们分别在数据持久化、XML处理、配置管理、数据校验和安全通信等方面有着广泛的应用。通过熟练掌握这些模块,开发者可以更高效地处理各种数据和任务。
相关推荐
![filetype](https://img-home.csdnimg.cn/images/20241231044930.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044901.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044930.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044955.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044930.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044901.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044930.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38599231
- 粉丝: 3
最新资源
- 设计模式:面向对象软件的复用基础与实例解析
- 开发指南:Microsoft Office 2007与Windows SharePoint Services
- DB2 Version 9 Command Reference for Linux, UNIX, Windows
- EJB技术详解:Java与J2EE架构中的企业级组件
- Spring整合JDO与Hibernate:Kodo的使用教程
- PS/2鼠标接口详解:物理连接与协议介绍
- SQL触发器全解析:经典语法与应用场景
- 在线优化Apache Web服务器响应时间
- Delphi函数全解析:AnsiResemblesText, AnsiContainsText等
- 基于SoC架构的Network on Chip技术简介
- MyEclipse 6 Java开发完全指南
- VBA编程基础:关键指令与工作簿工作表操作
- Oracle学习与DBA守则:通往成功的道路
- Windows Server 2003 DNS配置教程
- 整合JSF, Spring与Hibernate:构建实战Web应用
- 在Eclipse中使用HibernateSynchronizer插件提升开发效率