python讀取測(cè)試數(shù)據(jù)的多種方式
一、通過創(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庫讀取,代碼如下
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
二、通過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、命令行通過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}]
三、通過excel讀取
1、創(chuàng)建一個(gè)excel文件,隨便寫點(diǎn)數(shù)據(jù)
2、通過pandas讀取,當(dāng)然還有其他的很多庫,本人覺得這個(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)文章希望大家以后多多支持腳本之家!
- 從零學(xué)python系列之從文件讀取和保存數(shù)據(jù)
- python讀取excel指定列數(shù)據(jù)并寫入到新的excel方法
- python讀取txt文件并取其某一列數(shù)據(jù)的示例
- Python如何讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)
- python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例
- python 讀取.csv文件數(shù)據(jù)到數(shù)組(矩陣)的實(shí)例講解
- python讀取json文件并將數(shù)據(jù)插入到mongodb的方法
- Python實(shí)現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作示例
- python讀取各種文件數(shù)據(jù)方法解析
相關(guān)文章
詳解tensorflow載入數(shù)據(jù)的三種方式
這篇文章主要介紹了詳解tensorflow載入數(shù)據(jù)的三種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04tensorflow官方github預(yù)訓(xùn)練模型下載方式
這篇文章主要介紹了tensorflow官方github預(yù)訓(xùn)練模型下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02分享15 超級(jí)好用得 Python 實(shí)用技巧
這篇文章主要分享了15 超級(jí)好用得 Python 實(shí)用技巧,如果你對(duì)其中一個(gè)或多個(gè)感興趣,可以參考一下,希望對(duì)你能有所幫助2021-12-12Python爬蟲之對(duì)CSDN榜單進(jìn)行分析
這篇文章主要介紹了Python爬蟲之對(duì)CSDN榜單進(jìn)行分析,文章有詳細(xì)代碼,簡(jiǎn)單易懂,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2023-01-01

python實(shí)現(xiàn)word/excel/ppt批量轉(zhuǎn)pdf的示例代碼