- import yaml
- import json
- from configparser import ConfigParser
- from common.logger import logger
- class MyConfigParser(ConfigParser):
- # 重寫 configparser 中的 optionxform 函數(shù),解決 .ini 文件中的 鍵option 自動(dòng)轉(zhuǎn)為小寫的問題
- def __init__(self, defaults=None):
- ConfigParser.__init__(self, defaults=defaults)
- def optionxform(self, optionstr):
- return optionstr
- class ReadFileData():
- def __init__(self):
- pass
- def load_yaml(self, file_path):
- logger.info("加載 {} 文件......".format(file_path))
- with open(file_path, encoding='utf-8') as f:
- data = yaml.safe_load(f)
- logger.info("讀到數(shù)據(jù) ==>> {} ".format(data))
- return data
- def load_json(self, file_path):
- logger.info("加載 {} 文件......".format(file_path))
- with open(file_path, encoding='utf-8') as f:
- data = json.load(f)
- logger.info("讀到數(shù)據(jù) ==>> {} ".format(data))
- return data
- def load_ini(self, file_path):
- logger.info("加載 {} 文件......".format(file_path))
- config = MyConfigParser()
- config.read(file_path, encoding="UTF-8")
- data = dict(config._sections)
- # print("讀到數(shù)據(jù) ==>> {} ".format(data))
- return data
- ini_data = ReadFileData().load_ini('C:\\Users\\heguanghua\\Downloads\\pytestDemo-master\\config\\setting.ini')
- print(ini_data)
- json_data = ReadFileData().load_json('C:\\Users\\heguanghua\\Downloads\\pytestDemo-master\\data\\test_data.json')
- print(json_data)
- yml_data = ReadFileData().load_yaml('C:\\Users\\heguanghua\\Downloads\\pytestDemo-master\\data\\api_test_data.yml')
- print(yml_data)
運(yùn)行結(jié)果:
- D:\Users\heguanghua\AppData\Local\Programs\Python\Python35\python.exe C:/Users/heguanghua/Downloads/pytestDemo-master/common/read_data.py
- {'host': OrderedDict([('api_root_url', 'http://127.0.0.1:8000')]), 'mysql': OrderedDict([('MYSQL_HOST', '172.20.0.91'), ('MYSQL_PORT', '3305'), ('MYSQL_USER', 'root'), ('MYSQL_PASSWD', '123456'), ('MYSQL_DB', 'demo')])}
- [2020-07-10 15:25:13,371][read_data.py 35][INFO]: 加載 C:\Users\heguanghua\Downloads\pytestDemo-master\config\setting.ini 文件......
- {'user1': {'account': 'admin', 'password': '654321', 'message': '登錄成功'}, 'user2': {'account': 'admins', 'password': '654321', 'message': '登錄成功'}}
- [2020-07-10 15:25:13,372][read_data.py 28][INFO]: 加載 C:\Users\heguanghua\Downloads\pytestDemo-master\data\test_data.json 文件......
- [2020-07-10 15:25:13,372][read_data.py 31][INFO]: 讀到數(shù)據(jù) ==>> {'user1': {'account': 'admin', 'password': '654321', 'message': '登錄成功'}, 'user2': {'account': 'admins', 'password': '654321', 'message': '登錄成功'}}
- [2020-07-10 15:25:13,372][read_data.py 21][INFO]: 加載 C:\Users\heguanghua\Downloads\pytestDemo-master\data\api_test_data.yml 文件......
- {'test_update_user': [[4, '123456', '13500010014', '1', '深圳市寶安區(qū)', True, 0, '修改用戶信息成功'], [4, '123456', '1350001001', '1', '深圳市寶安區(qū)', False, 4008, '手機(jī)號(hào)格式不正確'], [111, '123456', '13500010014', '1', '深圳市寶安區(qū)', False, 4005, '用戶ID不存在']], 'test_get_all_user_info': [[True, '999999', '查詢成功!']], 'test_login_user': [['wintest', '123456', True, 0, '登錄成功'], ['測試test', '123456', False, 1003, '用戶名不存在']], 'test_delete_user': [['測試test', True, 0, '刪除用戶信息成功'], ['wintest3', False, 3006, '該用戶不允許刪除']], 'test_get_get_one_user_info': [['wintest4', True, 0, '查詢成功'], ['wintest1111', False, '1004', '查不到相關(guān)用戶']], 'test_register_user': [['測試test', '123456', '13599999999', '1', '深圳市寶安區(qū)', True, 0, '注冊(cè)成功'], ['測試test', '123456', '13599999999', '3', '深圳市寶安區(qū)', False, 2003, '輸入的性別只能是 0(男) 或 1(女)'], ['wintest4', '123456', '13599999999', '1', '深圳市寶安區(qū)', False, 2002, '用戶名已存在']]}
- [2020-07-10 15:25:13,385][read_data.py 24][INFO]: 讀到數(shù)據(jù) ==>> {'test_update_user': [[4, '123456', '13500010014', '1', '深圳市寶安區(qū)', True, 0, '修改用戶信息成功'], [4, '123456', '1350001001', '1', '深圳市寶安區(qū)', False, 4008, '手機(jī)號(hào)格式不正確'], [111, '123456', '13500010014', '1', '深圳市寶安區(qū)', False, 4005, '用戶ID不存在']], 'test_get_all_user_info': [[True, '999999', '查詢成功!']], 'test_login_user': [['wintest', '123456', True, 0, '登錄成功'], ['測試test', '123456', False, 1003, '用戶名不存在']], 'test_delete_user': [['測試test', True, 0, '刪除用戶信息成功'], ['wintest3', False, 3006, '該用戶不允許刪除']], 'test_get_get_one_user_info': [['wintest4', True, 0, '查詢成功'], ['wintest1111', False, '1004', '查不到相關(guān)用戶']], 'test_register_user': [['測試test', '123456', '13599999999', '1', '深圳市寶安區(qū)', True, 0, '注冊(cè)成功'], ['測試test', '123456', '13599999999', '3', '深圳市寶安區(qū)', False, 2003, '輸入的性別只能是 0(男) 或 1(女)'], ['wintest4', '123456', '13599999999', '1', '深圳市寶安區(qū)', False, 2002, '用戶名已存在']]}
聯(lián)系客服