hashlib,hmac,subprocess,configparser,xlrd,xlwt,xml模块基本功能
hashlib模块:加密
import hashlib
# 基本使用
cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))
print(cipher.hexdigest()) # 加密结果码
# 加盐
cipher = hashlib.md5()
cipher.update('前盐'.encode('utf-8'))
cipher.update('需要加密的数据'.encode('utf-8'))
cipher.update('后盐'.encode('utf-8'))
print(cipher.hexdigest()) # 加密结果码
# 其他算法
cipher = hashlib.sha3_256(b'')
print(cipher.hexdigest())
cipher = hashlib.sha3_512(b'')
print(cipher.hexdigest())
hmac模块:加密
# 必须加盐
cipher = hmac.new('盐'.encode('utf-8'))
cipher.update('数据'.encode('utf-8'))
print(cipher.hexdigest())
configparser模块:操作配置文件
# my.ini
[section1]
option1_1 = value1_1
option1_2 = value1_2
[section2]
option2_1 = value2_1
option2_2 = value2_2
import configparser
parser = configparser.ConfigParser()
# 读
parser.read('my.ini', encoding='utf-8')
# 所有section
print(parser.sections())
# 某section下所有option
print(parser.options('section_name'))
# 某section下某option对应的值
print(parser.get('section_name', 'option_name'))
# 写
parser.set('section_name', 'option_name', 'value')
parser.write(open('my.ini', 'w'))
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读
年终报表
教学部 市场部 咨询部 总计
Jan-19 10 15 5 30
Feb-19 10 15 5 30
Mar-19 10 15 5 30
Apr-19 10 15 5 30
May-19 10 15 5 30
Jun-19 10 15 5 30
Jul-19 10 15 5 30
Aug-19 10 15 5 30
Sep-19 10 15 5 30
Oct-19 10 15 5 30
Nov-19 10 15 5 30
Dec-19 10 15 5 30
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")
hashlib,hmac,subprocess,configparser,xlrd,xlwt,xml模块基本功能的更多相关文章
- 20 常用模块 hashlib hmac:加密 xml xlrd xlwt:excel读|写 configparser subprocess
hashlib模块:加密 加密: 1.有解密的加密方式 2.无解密的加密方式:碰撞检查 hashlib -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import ...
- configparser模块,subprocess 模块,xlrd,xlwt ,xml 模块,面向对象
1. configparser模块 2.subprocess 模块 3.xlrd,xlwt 4.xml 模块 5.面向对象 面向对象是什么? 是一种编程思想,指导你如何更好的编写代码 关注点在对象 具 ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块
一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...
- Learning-Python【21】:Python常用模块(4)—— re、logging、hashlib、subprocess
re 模块:与正则相关的模块 在使用 re 模块之前,需要先了解正则表达式(regular expression),描述了一种字符串匹配的模式(pattern),可以用来检查一个字符串是否含有某个子字 ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
- oop、configparser、xml模块
本节大纲:一:在python中,有两种编程思想.1:函数式编程.2:oop.无论是函数式编程还是oop都是相辅相成.并不是说oop比函数式编程就好.各有各的优缺点.在其他语言中java等只能以面向对象 ...
随机推荐
- 【密码技术】Part 4 SSL/TLS
01 SSL/TLS基本概念 02 TLS协议流程图
- springboot秒杀课程学习整理1-2
1)从数据库到前端,做了三层转换,最后统一返回给前端的格式 DO-> model: 放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型 ...
- LJN数理化生信奥队自传
LJN数理化生信奥队, 原名“LJN信奥队”,简称“ljnoit”. 联系方式: QQ:3046036317 QQ群:555088375 (Offical群) 701124785 (Vip群) 邮箱: ...
- storcli 简易使用介绍
MegaCli 是LSI公司官方提供的SCSI卡管理工具,由于LSI被收购变成了现在的Broadcom,所以现在想下载MegaCli,需要去Broadcom官网查找Legacy产品支持,搜索MegaR ...
- 记录配置xshell显示中文乱码的一次巧合
linux系统设置了中文UTF8,xshell终端也设置了中文UTF8,但xshell仍然无法显示中文,于是想起可能是字体显示问题. 设置楷体字体后,虽然文字方向还有问题,但乱码显示的问题解决了,方向 ...
- C#异常:未将对象引用设置到对象的实例。
异常:未将对象引用设置到对象的实例. 一般是定义的变量或者数组等,没有赋初始值. 赋初始值后问题解决.
- C# 索引和长度必须引用该字符串内的位置 LENGTH
今天遇到了 索引和长度必须引用该字符串内的位置 的问题. 原因是实用 Substring 对字符串进行了前五位的截取,但是忽略了字符串本身不足五位的情况. 如果不足五位,直接将整个字符串赋值过来,添加 ...
- Beta冲刺 1
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10093150.html 作业博客:https://edu.cnblogs.com/campus ...
- sprite kit -- 从入门到淡定
非常有趣的事情是接触到spritekit 真正认识他才会发现游戏开发在iOS端是如此便捷,LearnSpriteKitFromScratch 这本书详细到令人发指,入门好书.值得阅读.
- 《A Knowledge-Grounded Neural Conversation Model》
abstract 现在的大多数模型都可以被应用在闲聊场景下,但是还没有证据表明他们可以应用在更有用的对话场景下.这篇论文提出了一个知识驱动的,带有背景知识的神经网络对话系统,目的是为了在对话中产生更有 ...