python讀取測試數(shù)據(jù)的多種方式
一、通過創(chuàng)建.ini或.conf文件讀取
1、創(chuàng)建一個config.ini或者.conf文件,這種方法就是ini文件的讀取,如下
[api] url = www.taobao.com method = get [mysql] db=hello port=3306
2、使用python的configparser庫讀取,代碼如下
from configparser import ConfigParser import os conn=ConfigParser() #獲取ini文件的路徑 file_path = os.path.join(os.path.abspath('.'),'config.ini') conn.read(file_path) url=conn.get('api','url') method=conn.get('api','method') port=conn.get('mysql','port') print(url,method,port)
3、運行得到如下的值,讀取成功
www.taobao.com get 3306
二、通過yaml文件讀取
1、首先創(chuàng)建一個.yaml文件,這里我創(chuàng)建login_data.yaml,具體數(shù)據(jù)格式大家自行g(shù)oogle,這里我隨便編寫一些數(shù)據(jù)
- caseid: 1 method: post title: 正常登錄 url: api/v1/login data: username: test password: 123456 headers: Content-Type: application/json User-Agent: Mozilla/5.0 expected: 0
2、命令行通過pip install pyyaml安裝yaml包,直接上代碼
import yaml def read_file(file_path): with open(file_path, 'r', encoding='utf-8') as f: res = yaml.load(f,Loader=yaml.FullLoader) return res if __name__ == '__main__': d = read_file(r'..\test_data\login\login_data.yml') print(d)
3、運行結(jié)果如下,是一個列表
[{'caseid': 1, 'method': 'post', 'title': '正常登錄', 'url': 'api/v1/login', 'data': {'username': 'test', 'password': 123456}, 'headers': {'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0'}, 'expected': 0}]
三、通過excel讀取
1、創(chuàng)建一個excel文件,隨便寫點數(shù)據(jù)
2、通過pandas讀取,當然還有其他的很多庫,本人覺得這個最方便,直接轉(zhuǎn)化成dict方便使用
import pandas def excel_to_dic(filename): df = pandas.read_excel(filename) data_list = [] for i in df.index.values: data = df.iloc[i, [j for j in range(len(df.keys()))]].to_dict() data_list.append(data) return data_list if __name__ == '__main__': file = r'C:\aaa.xlsx'#之前創(chuàng)建的文件地址 data = excel_to_dic(file) print(data)
3、運行結(jié)果如下
[{'name': 'zhangsan', 'phone': 13112345678, 'age': 20}, {'name': 'lisi', 'phone': 13867543678, 'age': 30}, {'name': 'wangwu', 'phone': 15967845632, 'age': 40}]
可以看出結(jié)果是把excel表中的每一行數(shù)據(jù)轉(zhuǎn)換成了一個dict,更加方便以后使用!??!
到此這篇關(guān)于python讀取測試數(shù)據(jù)的多種方式的文章就介紹到這了,更多相關(guān)python讀取測試數(shù)據(jù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。