国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python Json數(shù)據(jù)文件操作原理解析

瀏覽:60日期:2022-07-26 10:07:53

引言

接口測試就是數(shù)據(jù)的測試,在測試之前,需要準備好測試數(shù)據(jù),而測試數(shù)據(jù)可以用數(shù)據(jù)庫、excel、txt和csv方式,當然還有一種方式,那就是使用json文件來儲存測試數(shù)據(jù)。常用的方式就是這些。

設計思路

python讀取json文件和讀取txt方式是一樣的,獲取路徑,判斷路徑是否存在,獲取文件名及絕對路徑,打開讀取數(shù)據(jù),提取關鍵數(shù)據(jù),關閉文件。具體流程,畫了一個草圖方便理解:

Python Json數(shù)據(jù)文件操作原理解析

具體代碼實現(xiàn)

@author: Leo @software: pycharm @file: operate_json.py @time: 2020/5/3 0003 9:01 @Desc: ’’’__author__ = ’Leo’ import osimport json # 獲取當前文件所在的絕對路徑curPath = os.path.abspath(os.path.dirname(__file__))print(curPath)rootPath = os.path.abspath(os.path.dirname(curPath))print(rootPath) config_file_name = r’./data/api_json’ class OperateJson(object): ''' 操作Json文件 ''' def __init__(self,file_name = None): if file_name: self.file_name = file_name else: self.get_file = config_file_name self.file_name = os.path.join(rootPath,self.get_file) print('文件名稱:%s'%self.file_name) self.data = self.read_json() def read_json(self): ''' 讀取json數(shù)據(jù) ''' with open(self.file_name,encoding=’utf8’) as fp: # 反序列化,從文件讀取(string轉dict) data = json.load(fp) fp.close() return data def get_keyword_data(self,key): ''' 讀取關鍵字 ''' return self.data[key]if __name__ == ’__main__’: # oj = OperateJson('../data/package.json') oj = OperateJson() print(oj.read_json()) print(oj.get_keyword_data(’api1’)) print(oj.get_keyword_data(’api1’)[’url’]) print(oj.get_keyword_data(’api1’)[’data’])

運行結果:

Python Json數(shù)據(jù)文件操作原理解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 温泉县| 昭通市| 忻州市| 顺平县| 林口县| 门头沟区| 惠安县| 乾安县| 耿马| 通化市| 将乐县| 孝昌县| 滕州市| 镇宁| 恩平市| 祁阳县| 沿河| 荔波县| 清河县| 黔江区| 右玉县| 博爱县| 沙雅县| 武义县| 灵川县| 拉萨市| 新沂市| 河南省| 岚皋县| 类乌齐县| 宁阳县| 卢氏县| 昌邑市| 松桃| 邻水| 利辛县| 夹江县| 休宁县| 冕宁县| 内丘县| 睢宁县|