python基础 configparser hashlib模块

configparser模块

 1 import configparser
 2 conf = configparser.ConfigParser()
 3 #生成一个配置文件,配置文件写入类似字典k:v格式,只是kv必须全部为字符串格式
 4 conf["DEFAULT"] = {"name" : "xxx",
 5                     "age" : '25',
 6                     "sal" : '30'}
 7 conf["set1"] = {}
 8 conf["set1"]["name"] = "xxx"
 9 conf["set1"]["age"] = '35'
10 conf["set1"]["sal"] = '45'
11 
12 with open("config.ini", "w") as config:
13     conf.write(config)
14 
15 #读取配置文件
16 data = configparser.ConfigParser()
17 data.read("config.ini")
18 print(data.sections()) #输出除DEFAULT外的第一级配置项名称
19 print(data["set1"]["name"]) #获取具体配置文件的值
20 print(data.defaults()) #获取默认配置项的所有信息
21 print(data["DEFAULT"]["age"]) #25

hashlib模块

import hashlib
#python3.x里面hashlib替代了md3 sha模块,有md5  sha1 sha128 256 512等加密模式
hash_data = hashlib.sha256()
hash_data.update(b"hello") #将hello进行sha256
print(hash_data.hexdigest()) #将sha256以后的数据以16进制格式显示
hash_data.update(b"wo jiu shi wo")
print(hash_data.hexdigest())

hash_data2 = hashlib.sha256()
hash_data2.update(b"hellowo jiu shi wo") #等于上一行的print结果,update是累加的
print(hash_data2.hexdigest())

猜你喜欢

转载自www.cnblogs.com/flags-blog/p/12013931.html