標籤:delete car nbsp word sys.argv class 操作 執行 系統
1.補充上篇中的os,sys模組的幾個小功能
import os,sysres1=os.system(‘ipconfig‘)#os.system(commad)執行作業系統命令,不返回執行結果res2=os.popen(‘ipconfig‘).read()#os.popen(commad)執行作業系統命令,返回執行結果,通過read方法可以讀取內容inpu=sys.argv#argv擷取執行該python檔案時的參數為list,如 ls -a 這裡的a就是參數if len(inpu)>1: if inpu[1]==‘-install‘: print(‘安裝成功‘) elif inpu[1]==‘-help‘: print(‘靠人不如靠己,別老help‘)else: print(‘錯了‘)
2.time,datetime模組
import time,datetimeprint(time.time())#擷取目前時間點的時間戳記print(time.localtime())#擷取目前時間點的時間元祖print(time.strftime(‘%Y-%m-%d %H:%M:%S‘))#擷取目前時間點的格式化時間print(datetime.datetime.now())#擷取目前時間點的格式化時間print(datetime.datetime.now()+datetime.timedelta(+3))#擷取目前時間點三天后的時間print(datetime.datetime.now()+datetime.timedelta(-3))#擷取目前時間點三天前的時間print(time.gmtime(1234556489))#時間戳記轉換成時間元祖print(time.mktime(time.gmtime(1234556489)))#時間元祖轉換成時間戳記print(time.strftime(‘%Y‘,time.gmtime(1234556489)))#時間元祖轉換成格式化時間print(time.strptime(‘2009‘,‘%Y‘))#格式化時間轉換成時間元祖def transfer(timestamp,format=‘%Y%m%d‘):#時間戳記格式化輸出 timetuple=time.gmtime(timestamp) print(time.strftime(format,timetuple))def transfer2(timesformat,format=‘%Y%m%d‘):#格式化時間轉換成時間戳記輸出 timetuple=time.strptime(timesformat,format) print(time.mktime(timetuple))transfer(231568465)transfer2(‘19770504‘)
3.hashlib模組
import hashlibmd=hashlib.md5()md.update(‘123456‘.encode())#一定要造待加密的字串後面加上“.encode”編碼,才能加密,否則報錯print(md.hexdigest())def md_sault(str,sault=‘123456‘):#加鹽,sault是鹽值,預設是123456 import hashlib str=str+sault md=hashlib.md5() md.update(str.encode()) print(md.hexdigest())md_sault(‘adsfad‘)
4.redis模組
import rediscoon=redis.Redis(host=‘192.168.217.128‘,password=‘123456‘,port=6379,db=1)print(coon.hget(‘Idcards‘,‘花校納‘).decode())#decode轉碼,這裡get到的資料是bytes格式的,需要轉碼coon.set(‘葛怕願‘,123456789)print(coon.get(‘戴鐵視‘).decode())coon.hset(‘Idcards‘,‘牛逼‘,123456789)print(coon.hget(‘Idcards‘,‘牛逼‘).decode())print(coon.keys())#資料庫上所有的keyres=coon.hgetall(‘Idcards‘)for i in res: print(i.decode(),‘:‘,res[i].decode())coon.delete(‘yxg‘)#刪除資料,yxg是key# 需求是把一個redis庫裡面所有的資料,匯入到另一個redis裡面# 1、r1 和r2 串連上r1和r2兩個資料庫# 2、 擷取到r1上面所有的key keys# 3、判斷key的類型,r.type(k),get hgetall .hset() .set()r1=redis.Redis(host=‘211.149.218.16‘,port=6378,password=‘123456‘,db=2)#擷取資料r2=redis.Redis(host=‘211.149.218.16‘,port=6378,password=‘123456‘,db=3)#寫資料keys = r1.keys()#r1資料庫上所有的keyfor k in keys: if r1.type(k)==b‘hash‘:#判斷是否為雜湊類型,因為redis裡面返回的資料都是bytes類型的, #所以在hash前面加上b hash_data = r1.hgetall(k)#擷取雜湊的類型的資料 for k2,v in hash_data.items():#迴圈剛才擷取到的字典 r2.hset(k,k2,v)#set雜湊類型的值 else: v = r1.get(k)#從r1裡面擷取值, r2.set(k,v)#set進去
5.json模組
import jsonnames={"yxg":123456,"yyy":654321,"zn":666}res=json.dumps(names)#字典轉json,dumps在mysql裡有這個概念,mysqldump匯出資料,這裡字典是python內建的,json是外來的,所以dumps是把字典匯出為jsonnew_names=json.loads(res)#json轉字典,與dumps相反,那邊是匯出,這裡就是匯入了,將外來的json匯入為dictprint(new_names)print(type(new_names))print(res)print(type(res))f=open(‘a.txt‘,‘w‘,encoding=‘utf-8‘)json.dump(names,f,ensure_ascii=False)#將字典寫入檔案為json格式,不需要用write,直接json.dump(dict_name,file_name,ensure_ascii=False)即可注意ensure_ascii=False是必須要的,json.dump()預設的是ascii編碼,並不認識中文,需要關閉這個ascii編碼f=open(‘a.txt‘,encoding=‘utf-8‘)res=json.load(f)#將json檔案內容轉換成字典格式,不需要用read,直接json.load(file_object)即可print(res)print(type(res))
6.mysql模組
import pymysqlfrom pymysql.cursors import DictCursorcoon=pymysql.connect(host=‘localhost‘,port=3306,user=‘root‘,password=‘root‘,db=‘test‘,charset=‘utf8‘)cur=coon.cursor(DictCursor)cur.execute(‘select * from stu‘)res=cur.fetchall()print(res)for i in res: print(i)cur.close()coon.close()
python常用模組基礎