1. 程式人生 > >python的各種模組

python的各種模組

第三方模組安裝方式 1. pip install xxx 2. 知道到對應的模組 .pypi網站,下載.whl模組 這裡寫圖片描述 這裡寫圖片描述 這裡寫圖片描述 3. 解壓 .tar 檔案。開啟解壓後的資料夾 這裡寫圖片描述 進入命令列之後,執行python setup.py install\ 此方式將以前安裝的模組解除安裝掉,再重新安裝

hashlib ——–加密模組 無論需要加密的字串有多長,加密之後都是32位字串

import hashlib
password='123456'
m = hashlib.md5(password.encode())#字串轉2進位制
#print(dir(m)) #產看模組下有哪些方法,不記得方法名的時候用這個來檢視
print(m.hexdigest())

這裡寫圖片描述 撞庫:一些加密後的密文被存在資料庫,當存在含有的密文時,裡面有對應的明文,就可以知道此密文對應的明文,但是如果資料庫裡面沒有對應的密文則無法得到對應的明文,即輸入的密碼。 一般在原密碼後面隨意的加字串,再加密,就可以得到比較複雜的密文,這種過程稱為“加鹽”。

password='12345'
def my_md5(s:str,salt=None):
    s=str(s)
    if salt:
        s=s+salt
    m=hashlib.md5(s.encode())
    return m.hexdigest()
m=my_md5(password,'jiukkkl'
) print(m)

漢字轉拼音模組

import xpinyin
s=xpinyin.Pinyin()
print(s.get_pinyin('饕鬄','$'))   #$裡面的內容表示使用什麼進行連線,如果沒有預設使用’-‘連線

這裡寫圖片描述

mysql連線模組 顯示所有的表,修改SQL語句就可以進行其他操作

host='XXX.XX.X.XX'
user='jxz'
password='123456' #密碼只能是字串
db='jxz'
port=3306#埠號只能寫int型別
charset='utf8'#只能寫utf8,不能寫utf-8
conn = pymysql.connect(host=host,password=password,charset=charset,user=user,db=db,port=port)#建立連線
cur= conn.cursor() #建立遊標 cur.execute('show tables;')#只是幫你執行sql語句 print(cur.fetchall())#獲取資料庫裡面的所有的結果 cur.close() conn.close()

python的excel實現

import xlwt
book=xlwt.Workbook()
sheet=book.add_sheet('sheet1')
sheet.write(0,0,'id')
sheet.write(0,1,'username')
sheet.write(0,2,'password')

sheet.write(1,0,'1')
sheet.write(1,1,'niuhanyang')
sheet.write(1,2,'123456')

sheet.write(2,0,'2')
sheet.write(2,1,'yangyang')
sheet.write(2,2,'123457')
book.save('stu.xls')

讀excel裡面的內容

import xlrd
book=xlrd.open_workbook('stu.xls')
sheet =book.sheet_by_index(0)
sheet =book.sheet_by_name('sheet1')
print(sheet.nrows)     #excel裡面有多少行
print(sheet.ncols)     #excel 裡面有多少列

print(sheet.cell(0,1).value)  #獲取指定單元格內容
print(sheet.cell(0,2).value)

print(sheet.row_values(0))   #獲取整行的內容
print(sheet.col_values(1))   #獲取整列的內容

for i in range(sheet.nrows):    #迴圈顯示每一行
    print(sheet.row_values(i))

這裡寫圖片描述 修改excel:

import xlrd
from xlutils import copy
book=xlrd.open_workbook('stu.xls')  #先開啟excel
new_book=copy.copy(book)     #再複製一個excel
sheet=new_book.get_sheet(0)   #獲取sheet
sheet.write(0,1,'你局方')
sheet.write(1,1,'芳芳')
new_book.save('stu.xls')