python讀取測(cè)試數(shù)據(jù)的多種方式
一、通過(guò)創(chuàng)建.ini或.conf文件讀取
1、創(chuàng)建一個(gè)config.ini或者.conf文件,這種方法就是ini文件的讀取,如下
[api] url = www.taobao.com method = get [mysql] db=hello port=3306
2、使用python的configparser庫(kù)讀取,代碼如下
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、運(yùn)行得到如下的值,讀取成功
www.taobao.com get 3306
二、通過(guò)yaml文件讀取
1、首先創(chuàng)建一個(gè).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、命令行通過(guò)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、運(yùn)行結(jié)果如下,是一個(gè)列表
[{'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}]
三、通過(guò)excel讀取
1、創(chuàng)建一個(gè)excel文件,隨便寫點(diǎn)數(shù)據(jù)

2、通過(guò)pandas讀取,當(dāng)然還有其他的很多庫(kù),本人覺(jué)得這個(gè)最方便,直接轉(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、運(yùn)行結(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)換成了一個(gè)dict,更加方便以后使用?。。?br/>
到此這篇關(guān)于python讀取測(cè)試數(shù)據(jù)的多種方式的文章就介紹到這了,更多相關(guān)python讀取測(cè)試數(shù)據(jù)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信