模塊之hashlib、hmac、configparser、subprocess、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
# 註釋:該配置文件中,值直接書寫,但有四種類型
# -- int float boolean str
# section
[server] 
# name:option | value:mysql
name = mysql 
version = 20000

[client]
name = owen
adress = 192.168.11.174
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)
# 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('系統默認編碼')

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")
相關文章
相關標籤/搜索