DAY19、日常模块
一、hashlib模块:加密
1、基本使用:
import hashlib
cipher = hashlib.md5('需要加密的数据(二进制形式)'.encode('utf-8'))
print(cipher.hexdigest()) #加密结果码
2、加盐
cipher = hashlib.md5()
cipher.update('前盐'.encode('utf-8'))
cipher.update('需要加密的数据'.encode('utf-8'))
cipher.update('后盐'.encode('utf-8'))
print(cipher.hexdigest())
3、其他算法
cipher = hashlib.sha3_256(b'')
print(cipher.hexdigest())
cipher = hashlib.sha3_512(b'')
print(cipher.hexdigest)
二、hmac模块:加密
import hmac
和md5()区别 hashlib.md5():
-- 可以有初始参数,可以没有初始参数
-- 可以通过update再添加新内容
hmac.new():
-- 必须有初始参数
-- 可以通过update再添加新内容
cipher = hmac.new('盐'.encode('utf-8'))
cipher.update('数据'.encode('utf-8'))
print(cipher.hexdigest())
三、configparser模块:操作配置文件
from configparser import ConfigParser
# 初始化配置文件的操作对象
parser = ConfigParser()
# 读
parser.read('my.ini', encoding='utf-8')
# 大分类:section
print(parser.sections())
# 某分类下的keys:option
print(parser.options('server'))
print(parser.options('client'))
# for section in parser.sections():
# print(parser.options(section))
# 获取某section下某option的具体值
# res = parser.get('server', 'version')
res = parser.getfloat('server', 'version')
print(res, type(res)) # 写
parser.set('server', 'version', '20000') # 写到内存
parser.write(open('my.ini', 'wt'))
四、subprocess模块:操作shell命令
import subprocess
order = subprocess.Popen('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
suc_res = order.stdout.read().decode('系统默认编码')
err_res = order.stderr.read().decode('系统默认编码') order = subprocess.run('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
suc_res = order.stdout.decode('系统默认编码')
err_res = order.stderr.decode('系统默认编码')
五、xlrd模块:excel读
import xlrd
# 读取文件
work_book = xlrd.open_workbook("机密数据.xlsx")
# 获取所有所有表格名称
print(work_book.sheet_names())
# 选取一个表
sheet = work_book.sheet_by_index(1)
# 表格名称
print(sheet.name)
# 行数
print(sheet.nrows)
# 列数
print(sheet.ncols)
# 某行全部
print(sheet.row(6))
# 某列全部
print(sheet.col(6))
# 某行列区间
print(sheet.row_slice(6, start_colx=0, end_colx=4))
# 某列行区间
print(sheet.col_slice(3, start_colx=3, end_colx=6))
# 某行类型 | 值
print(sheet.row_types(6), sheet.row_values(6))
# 单元格
print(sheet.cell(6,0).value) # 取值
print(sheet.cell(6,0).ctype) # 取类型
print(sheet.cell_value(6,0)) # 直接取值
print(sheet.row(6)[0])
# 时间格式转换
print(xlrd.xldate_as_datetime(sheet.cell(6, 0).value, 0))
六、xlwt模块:excel写
import xlwt
# 创建工作簿
work = xlwt.Workbook()
# 创建一个表
sheet = work.add_sheet("员工信息数据")
# 创建一个字体对象
font = xlwt.Font()
font.name = "Times New Roman" # 字体名称
font.bold = True # 加粗
font.italic = True # 斜体
font.underline = True # 下划线
# 创建一个样式对象
style = xlwt.XFStyle()
style.font = font
keys = ['Owen', 'Zero', 'Egon', 'Liuxx', 'Yhh']
# 写入标题
for k in keys:
sheet.write(0, keys.index(k), k, style)
# 写入数据
sheet.write(1, 0, 'cool', style)
# 保存至文件
work.save("test.xls")
七、xml模块
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data> import xml.etree.ElementTree as ET
# 读文件
tree = ET.parse("xmltest.xml")
# 根节点
root_ele = tree.getroot()
# 遍历下一级
for ele in root_ele:
print(ele) # 全文搜索指定名的子标签
ele.iter("标签名")
# 非全文查找满足条件的第一个子标签
ele.find("标签名")
# 非全文查找满足条件的所有子标签
ele.findall("标签名") # 标签名
ele.tag
# 标签内容
ele.text
# 标签属性
ele.attrib # 修改
ele.tag = "新标签名"
ele.text = "新文本"
ele.set("属性名", "新属性值") # 删除
sup_ele.remove(sub_ele) # 添加
my_ele=ET.Element('myEle')
my_ele.text = 'new_ele'
my_ele.attrib = {'name': 'my_ele'}
root.append(my_ele) # 重新写入硬盘
tree.write("xmltest.xml")
DAY19、日常模块的更多相关文章
- python学习 day19 configparser模块 os模块 subprocess模块
上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...
- day19 python模块 json模块 pickle模块
day19 python 一.序列化模块 序列类型: 列表 字符串 元组 bytes 序列化: 特指字符串和bytes, 就是把其他的数据类型转化成序列的数据类型的过程 dic = ...
- day19:os模块&shutil模块&tarfile模块
os模块:对系统进行操作(6+3) system popen listdir getcwd chdir environ / name sep linesep import os #### ...
- day19其他模块
collections模块 详细内容 http://www.cnblogs.com/Eva-J/articles/7291842.html 1.namedtuple: 生成可以使用名字来访问元素内容的 ...
- day19:常用模块(collections,time,random,os,sys)
1,正则复习,re.S,这个在用的最多,re.M多行模式,这个主要改变^和$的行为,每一行都是新串开头,每个回车都是结尾.re.L 在Windows和linux里面对一些特殊字符有不一样的识别,re. ...
- day19常用模块2
常用模块21 shelve模块 也是一种序列化方式 使用方法 1.open sl = shelve.open("shelvetest.txt") ...
- os模块、os.path模块、shutil模块、configparser模块、subprocess模块
一.os模块 os指的是操作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd() 获取当前文件所在的文件夹路径 os.chdir() ...
- os模块,os.path模块,subprocess模块,configparser模块,shutil模块
1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...
- os模块-subprocess 模块- configpaser 模块
一. os 模块 主要用于处理与操作系统相关操作,最常用文件操作 使用场景:当需要操作文件及文件夹(增,删,查,改) os.getcwd() 获取当前工作目录 os.chdir('dirname') ...
随机推荐
- Devexpress 常用的功能
一 .GridControl的删除操作 private void rILinkEditInfoDel_Click(object sender, EventArgs e) { if (XtraMessa ...
- JDK源码分析(11)之 BlockingQueue 相关
本文将主要结合源码对 JDK 中的阻塞队列进行分析,并比较其各自的特点: 一.BlockingQueue 概述 说到阻塞队列想到的第一个应用场景可能就是生产者消费者模式了,如图所示: 根据上图所示,明 ...
- 【转载】假设有以下代码 String s = “hello”; 阿里巴巴笔试题
原文链接点这里 equals 源码如下: 分析: //true equal用于比较两个对象的值是否相同,和内存地址无关
- CentOS6.8 安装node.js npm
环境:CentOS6.8_X64系统 一.到官方下载最新的编译好的安装文件,目前是6.9.4. $>cd /usr/local/src #定位到这个目录,下载的文件会在这个目录#使用wget下载 ...
- 【网摘】C#中TransactionScope的使用方法和原理
时间 2013-08-12 19:59:34 51CTO推荐博文 原文 http://cnn237111.blog.51cto.com/2359144/1271600 在.net 1.1的时代,还 ...
- js实现复制文本内容到剪切板
function copyUrl() { var Url2=document.getElementById("url").innerText; var oInput = docum ...
- W3C 代码标准规范
W3C通过设立领域(Domains)和标准计划(Activities)来组织W3C的标准活动,围绕每个标准计划,会设立相关的W3C工作组织(包括工作组.社区组.商务组等).W3C会根据产业界的标准需求 ...
- [笔记]记录原开发工作在base命名空间下扩展的属性与方法
前言 该笔记只是为了记录以前开发使用的方式. 处理命名空间namespace /** * 处理命名空间 * @param {string} 空间名称,可多个 * @return {object} 对象 ...
- WordCount结对编程
合作者:201631062602,201631062114 代码地址:https://gitee.com/Changyu-Guo/pairing_project 作业链接:https://www.cn ...
- Lua rawget rawset newindex 函数定义和例子
在绝大多数情况下,我们都不会用到rawget和rawset. 本文的运行环境:lua 5.3 for windows rawset 赋值操作 rawset是在设置值的过程,进行处理,比如:当某个值改变 ...