模块三
一、hashlib:加密
#基本使用
import hashlib
cipher = hashlib.md5('需要加密的數據的二進制形式'.encoding='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.hexdiges())#加密結果碼
#其他算法
cipher = hashlib.sha3_256(b'')
print(cipher.hexdigest())
cipher = hashlib.sha3_512(b'')
print(cipher.hexdigest())
二、hmac:加密
#必須加鹽
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'))
四、subprocess模塊:操作shell命令
# 寫
parser.set('section_name', 'option_name', 'value')
parser.write(open('my.ini', 'w'))
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")
轉載于:https://www.cnblogs.com/Mr-bear/articles/10712889.html
總結
- 上一篇: 婴儿试管一次多少钱啊?
- 下一篇: 剑指offer-丑数