测试自动化学习7
python讀取Excel
import xlrdbook = xlrd.open_workbook('my_user-bak.xls') sheet = book.sheet_by_index(0) print(sheet.row_values(0)) # 某一行數(shù)據(jù) print(sheet.col_values(0)) # 某一列數(shù)據(jù) print(sheet.cell(0,0).value) # 某個單元格的數(shù)據(jù) print(sheet.cell(1,2).value) # 某個單元格的數(shù)據(jù) print(sheet.nrows) # 總共多少行 print(sheet.ncols) # 總共多少列print('----') for i in range(sheet.nrows): # 打印所有行print(sheet.row_values(i))?
python修改Excel內(nèi)容
import xlrd from xlutils import copy""" 1.用xlrd模塊打開Excel 2.用xlutils模塊里面的copy復(fù)制一份 3.獲取到sheet頁 4.修改 """book = xlrd.open_workbook('students.xls') new_book = copy.copy(book) sheet = new_book.get_sheet(0) sheetr = book.sheet_by_index(0) sheet.write(0,6,'年齡階段')for i in range(1, sheetr.nrows):if sheetr.cell(i,3).value < 18:sheet.write(i, 6, '騷年')elif 18 <= sheetr.cell(i,3).value < 30:sheet.write(i, 6, '成年人開車')else:sheet.write(i, 6, '老當(dāng)益壯')new_book.save('students.xls')?
python發(fā)郵件
import yagmailuser = 'xxx@qq.com' passwd = 'xxxxx' # 在系統(tǒng)賬戶設(shè)置開啟授權(quán)碼訪問,不可直接使用qq密碼登錄 smtp_host = 'smtp.qq.com'mail = yagmail.SMTP(user=user,password=passwd,host=smtp_host,smtp_ssl=True) # 連上郵箱 mail.send(to='@qq.com',cc='xxxaa@qq.com',subject='好好睡',contents='多吃多吃',attachments=['test.py', '寫日志.py']) # 多個人傳入list?
操作Redis,可以使用rdm圖形軟件操作輔助觀察,也可以直接用python操作
import redishost = '127.0.0.1' passwd = 'xxxxx'r = redis.Redis(host=host, password=passwd, db=15, decode_responses=True) # 選擇第幾個db,一般0-15 # decode_responses=True,返回的就不是bytes類型了,就是字符串了# string類型 # r.set('session_id_dd','s1243sdfsdfs') #新增和修改 # # result = r.get('session_id_') # # r.delete('session_id_dd') #刪除指定的key # # r.flushdb() #清除當(dāng)前數(shù)據(jù)庫里面所有的key # # r.flushall() #清除所有數(shù)據(jù)庫里面的所有key # print(r.keys())#獲取到當(dāng)前數(shù)據(jù)庫里面有哪些key # print(result)# print(result.decode()) #decode是把bytes類型變成字符串# 寫一個不存在的key,會怎樣# hash類型# print(r.hget('szz_stu','dsk'))print(r.hset('szz_stu', 'gxn', 'sdfsdfsfsf')) # r.hdel('szz_stu','gxn') r.hmset('szz_stu', {"ldd": 'dsk', 'cwl': 'brf'}) # 批量往hash類型里面set數(shù)據(jù)# r.hset('szz_stu','gxn','{"money":11111,"session_id":"xxxxxxx"}') print(r.hgetall('szz_stu')) print(r.type('szz_stu'))# 把a的數(shù)據(jù),全部遷移到b上面# 1、連上aredis和bredis # 2、從aredis里面獲取到所有的key [ ] # 3、判斷key的類型,如果是string類型,用get獲取數(shù)據(jù),set到新的bredis里面# 遷移redis host = '127.0.0.1' passwd = 'xxx'r = redis.Redis(host=host, password=passwd, db=15, decode_responses=True) # 0-15 r2 = redis.Redis(host=host, password=passwd, db=15, decode_responses=True, port=6378) # 0-15 for k in r.keys():if r.type(k) == 'string':value = r.get(k)r2.set(k, value)elif r.type(k) == 'hash':value = r.hgetall(k) # 首先獲取到hashkey里面的所有數(shù)據(jù)r2.hmset(k, value) # 然后把所有的數(shù)據(jù)set進(jìn)去 # 管道# p = r.pipeline() #新建了一個管道,操作比較多的時候用管道 # p.set('abc','aa') # p.get('xx') # p.hget('xxx') # p.execute() #執(zhí)行?
nnlog寫日志模塊
import nnloglog = nnlog.Logger('test.log', level='error', backCount=5, when='S') # D H M S log.debug('返回結(jié)果...') # 一些調(diào)試信息,看變量值這些 log.info('info...') # 一些提示信息 log.warning('waring') # 出警告了 log.error('error...') # 出錯的時候打印的# log.surprise()?
request模塊調(diào)用接口,可以實現(xiàn)get、post、下載等
import requests# url = 'http://api.nnzhp.cn/api/user/stu_info' # d = {'stu_name':'礦泉水2'} # # result = requests.get(url, d) # print(result.json()) # 結(jié)果轉(zhuǎn)成字典 # print(result.text) # 結(jié)果轉(zhuǎn)成字符串方便寫入文件# url = 'http://api.nnzhp.cn/api/user/login' # data = {'username':'niuhanyang', 'passwd':'aA123456'} # req = requests.post(url, data) # print(req.json()) url = 'http://api.nnzhp.cn/api/user/gold_add' data = {'stu_id': 2, 'gold': 1111} # cookie = {'niuhanyang':'b500d8a2f545102c8afa85135224149c'} cookie2 = {'cookie': 'niuhanyang=b500d8a2f545102c8afa85135224149c'} # 這種方式抓包到數(shù)據(jù)可以直接粘貼,不用轉(zhuǎn)k-v r = requests.post(url, data, headers=cookie2) print(r.text)#入?yún)⑹莏son的 # data = { # "name": "礦泉水33333", # "grade": "雙子座", # "phone": "12123624321", # "sex": "未知", # "addr": "天通苑", # "age": 38 # } # # url = 'http://api.nnzhp.cn/api/user/add_stu' # req = requests.post(url,json=data) # print(req.text)# url='http://api.nnzhp.cn/api/file/file_upload' # data = {'file':open('上周作業(yè).py','rb')} # req = requests.post(url,files=data) # print(req.text)# url = 'http://aliuwmp3.changba.com/userdata/userwork/12107482.mp3' url='https://aliimg.changba.com/cache/photo/18189396_640_640.jpg' req = requests.get(url) with open('aqmm.jpg','wb') as fw:fw.write(req.content)?
模塊1、一個python文件就是一個模塊。
2、
1、自己寫的
2、標(biāo)準(zhǔn)模塊,os time random hashlib
3、第三方模塊 pymysql xlwt
1、import模塊的實質(zhì)就是把這個python文件執(zhí)行了一遍(調(diào)試模塊代碼時,寫在 if __name__ == '__main__': 下面的調(diào)試執(zhí)行代碼,在import的時候不會被執(zhí)行) 2、查找模塊的順序
1、導(dǎo)入模塊的時候首先從當(dāng)前目錄下找
2、如果當(dāng)前目錄下沒有,就是去python的環(huán)境變量里面找
Mac OX 設(shè)置pip國內(nèi)鏡像,下載速度超快 $ vim ~/.pip/pip.conf[global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com
?
?轉(zhuǎn)載于:https://www.cnblogs.com/fatenet/p/10879092.html
總結(jié)
- 上一篇: 分页类与前台和后台的调用方法
- 下一篇: 电子统计台账:垂直流水账格式数据的导入