day 19 还是模块

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

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

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

xml模块

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")
View Code
<?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>
View Code

猜你喜欢

转载自www.cnblogs.com/wangyisen/p/10721028.html