一.加密模块

  1.加密方式:

    1.有解密的加密方式

    2.无解密的加密方式,碰撞检查

      1.不同数据加密后的结果一定不一致

      2.相同数据的加密结果一定是一致

  2.hashlib模块

    1.基本使用

    cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))

    print(cipher.hexdigest()) # 加密结果码

    2.加盐

    cipher = hashlib.md5()

    cipher.update('前盐'.encode('utf-8'))

    cipher.update('需要加密的数据'.encode('utf-8'))

    cipher.updata('后盐'.encode('utf-8))

    print(cipher.hexdigest()) # 加密结果码

    3.其他算法

    cipher = hashlib.sha3_256(b'')

    print(cipher.hexdigest())

    cipher = hashlib.sha3_512(b'')

    print(cipher.hexdigest())

  3.hmac模块

    # 必须加盐

    cipher = hmac.new('盐'.encode('utf-8'))

    cipher.updata('数据'.encode('utf-8'))

    print(cipher.hexdigest())

二.操作配置文件:configparser模块

  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'))

三.操作shell命令:subprocess模块

  import subprocess
  order = subprocess.Popen('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  # order.stdout 流对象,order.stdout.read()来获取操作的信息字符串
  suc_res = order.stdout.read().decode('系统默认编码')
  err_res = order.stderr.read().decode('系统默认编码')

  # stdout:存放指令执行成功的信息管道 | stderr 存放指令执行失败的信息管道
  order = subprocess.run('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  # order.stdout 是字符串信息,就是Popen下order.stdout.read()
  suc_res = order.stdout.decode('系统默认编码')
  err_res = order.stderr.decode('系统默认编码')

四.Excel读:xlrd模块

  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))

五.Excel写:xlwt模块

  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")

Day 20 常用模块(三)的更多相关文章

  1. Python之常用模块三(面向对象相关的三个模块)

    hashlib.configparser.logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...

  2. Python常用模块(三)

    一.shelve模块 shelve也是一种序列化方式,在python中shelve模块提供了基本的存储操作,shelve中的open函数在调用的事和返回一个shelf对象,通过该对象可以存储内容,即像 ...

  3. python运维开发常用模块(三)DNS处理模块dnspython

    1.dnspython模块介绍: dnspython(http://www.dnspython.org/)是Python实现的一个DNS 工具包,它支持几乎所有的记录类型,可以用于查询.传输并动态更新 ...

  4. DAY20 常用模块(三)

    一.加密模块 1.加密方式: 1.有解密的加密方式 2.无解密的加密方式,碰撞检查 1.不同数据加密后的结果一定不一致 2.相同数据的加密结果一定是一致 2.hashlib模块 1.基本使用 ciph ...

  5. python 15 常用模块三 re模块

    一.正则模块 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹 ...

  6. 20 常用模块 hashlib hmac:加密 xml xlrd xlwt:excel读|写 configparser subprocess

    hashlib模块:加密 加密: 1.有解密的加密方式 2.无解密的加密方式:碰撞检查 hashlib -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import ...

  7. Python学习 :常用模块(三)----- 日志记录

    常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...

  8. 20 Python 常用模块

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  9. nginx常用模块(三)

    Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...

随机推荐

  1. FZU-Problem 2294 Uint47 calculator

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2294 题意:按照所给负号进行赋值.加.减乘.除和取余的操作. 解题思路:用map来存储字符串与值之间的对应关系 ...

  2. [工控安全]西门子S7-400 PLC固件逆向分析(一)

    不算前言的前言:拖了这么久,才发现这个专题没有想象中的简单,学习的路径大致是Step7->S7comm->MC7 code->firmware,我会用尽量简短的语言把前两部分讲清楚, ...

  3. Problem B: STL——集合运算

    Description 集合的运算就是用给定的集合去指定新的集合.设A和B是集合,则它们的并差交补集分别定义如下: A∪B={x|x∈A∨x∈B} A∩B={x|x∈A∧x∈B} A-B={x|x∈A ...

  4. 第十三周翻译-《Pro SQL Server Internals, 2nd edition》

    <Pro SQL Server Internals, 2nd edition> 作者:Dmitri Korotkevitch 翻译:赖慧芳 译文: 聚集索引 聚集索引指示表中数据的物理顺序 ...

  5. 配置Nim的默认编译参数 release build并运行

    配置Nim的默认编译参数 release build并运行 默认情况下nim编译是debug build,如果需要release build, 需要加上-d:release , release编译的命 ...

  6. 判断一个js对象是否是Array

    今天在做题时遇到这个问题,找答案的时候,发现评论里大神好多 . 在开发中,我们经常需要判断某个对象是否为数组类型,总结判断某个对象是否是数组的方法. 1.typeof 操作符 对于Function, ...

  7. python之django基础

    看了不是同一期的视频,发现9期老师线性引入的方式,讲得django更加易于理解掌握. 抱歉的是,笔记没有整理就发上来了.希望看到的人不要被我带偏. 1. 新建Django项目 命令行创建: djang ...

  8. iftop流量监控工具

    下载iftop工具的源码包 # wget http:oss.aliyuncs.com/aliyunecs/iftop-0.17.tar.gz 安装所需的依赖包 # yum -y install gcc ...

  9. Problem: Time(一道水却有意思的题

    Problem Description Digital clock use 4 digits to express time, each digit is described by 3*3 chara ...

  10. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...