zoukankan      html  css  js  c++  java
  • python接口自动化读取json、yaml、ini文件

    import yaml
    import json
    from configparser import ConfigParser
    from common.logger import logger
    
    
    class MyConfigParser(ConfigParser):
        # 重写 configparser 中的 optionxform 函数,解决 .ini 文件中的 键option 自动转为小写的问题
        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("读到数据 ==>>  {} ".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("读到数据 ==>>  {} ".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("读到数据 ==>>  {} ".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)
    

    运行结果:

    D:UsersheguanghuaAppDataLocalProgramsPythonPython35python.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:UsersheguanghuaDownloadspytestDemo-masterconfigsetting.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:UsersheguanghuaDownloadspytestDemo-masterdata	est_data.json 文件......
    [2020-07-10 15:25:13,372][read_data.py 31][INFO]: 读到数据 ==>>  {'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:UsersheguanghuaDownloadspytestDemo-masterdataapi_test_data.yml 文件......
    {'test_update_user': [[4, '123456', '13500010014', '1', '深圳市宝安区', True, 0, '修改用户信息成功'], [4, '123456', '1350001001', '1', '深圳市宝安区', False, 4008, '手机号格式不正确'], [111, '123456', '13500010014', '1', '深圳市宝安区', 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', '查不到相关用户']], 'test_register_user': [['测试test', '123456', '13599999999', '1', '深圳市宝安区', True, 0, '注册成功'], ['测试test', '123456', '13599999999', '3', '深圳市宝安区', False, 2003, '输入的性别只能是 0(男) 或 1(女)'], ['wintest4', '123456', '13599999999', '1', '深圳市宝安区', False, 2002, '用户名已存在']]}
    [2020-07-10 15:25:13,385][read_data.py 24][INFO]: 读到数据 ==>>  {'test_update_user': [[4, '123456', '13500010014', '1', '深圳市宝安区', True, 0, '修改用户信息成功'], [4, '123456', '1350001001', '1', '深圳市宝安区', False, 4008, '手机号格式不正确'], [111, '123456', '13500010014', '1', '深圳市宝安区', 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', '查不到相关用户']], 'test_register_user': [['测试test', '123456', '13599999999', '1', '深圳市宝安区', True, 0, '注册成功'], ['测试test', '123456', '13599999999', '3', '深圳市宝安区', False, 2003, '输入的性别只能是 0(男) 或 1(女)'], ['wintest4', '123456', '13599999999', '1', '深圳市宝安区', False, 2002, '用户名已存在']]} 
    
    进程已结束,退出代码0
    

    详见此链接:https://blog.csdn.net/qq_41891186/article/details/103762138

  • 相关阅读:
    bzoj-2748 2748: [HAOI2012]音量调节(dp)
    bzoj-2338 2338: [HNOI2011]数矩形(计算几何)
    bzoj-3444 3444: 最后的晚餐(组合数学)
    codeforces 709E E. Centroids(树形dp)
    codeforces 709D D. Recover the String(构造)
    codeforces 709C C. Letters Cyclic Shift(贪心)
    codeforces 709B B. Checkpoints(水题)
    codeforces 709A A. Juicer(水题)
    Repeat Number
    hdu 1003 Max Sum (动态规划)
  • 原文地址:https://www.cnblogs.com/hghua/p/13279404.html
Copyright © 2011-2022 走看看