设为首页 加入收藏

TOP

Python-14-常用模块(五)
2019-07-15 16:09:59 】 浏览:216
Tags:Python-14- 常用 模块
ge', '18'), ('is_admin', 'true'), ('salary', '31')] # 查看标题section1下user的值=>字符串格式 val = config.get('section1', 'user') print(val) # egon # 查看标题section1下age的值=>整数格式 val1 = config.getint('section1', 'age') print(val1) # 18 # 查看标题section1下is_admin的值=>布尔值格式 val2 = config.getboolean('section1', 'is_admin') print(val2) # True # 查看标题section1下salary的值=>浮点型格式 val3 = config.getfloat('section1', 'salary') print(val3) # 31.0

4. 修改

import configparser

config = configparser.ConfigParser()
config.read('confile', encoding='utf-8')

# 删除整个标题section2
config.remove_section('section2')

# 删除标题section1下的某个k1和k2
config.remove_option('section1', 'k1')
config.remove_option('section1', 'k2')

# 判断是否存在某个标题
print(config.has_section('section1'))

# 判断标题section1下是否有user
print(config.has_option('section1', 'user'))

# 添加一个标题
config.add_section('egon')

# 在标题egon下添加name=egon,age=18的配置
config.set('egon', 'name', 'egon')
config.set('egon', 'age', 18)  # 报错,必须是字符串

# 最后将修改的内容写入文件,完成最终的修改
config.write(open('confile', 'w'))

十一、hashlib模块

1. 什么是hash

hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法),该算法接受传入的内容,经过运算得到一串hash值。

2. hash值的特点

  • 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验
  • 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码
  • 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的

3. 使用

import hashlib

ha = hashlib.sha256()
ha.update('hello'.encode('utf-8'))
print(ha.hexdigest())

m = hashlib.sha256('sb'.encode('utf-8'))
m.update('hello'.encode('utf-8'))
print(ha.hexdigest())

 

首页 上一页 2 3 4 5 6 7 下一页 尾页 5/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用Python递归做个多层次的文件执行 下一篇数据库-高级部分

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目