Bootstrap

python读取配置文件

import json
import os
import yaml
from configparser import ConfigParser



def get_json_data(path):
    json_data_path = os.path.abspath(path)
    with open(json_data_path, 'r', encoding='utf-8') as f:
        json_data = json.load(f)
        return json_data
# json_data_path


def get_yaml_data(path):
    yaml_data_path = os.path.abspath(path)
    with open(yaml_data_path, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f.read())
        return data

def get_env_info(fields,env_info_path):
    env_info = get_yaml_data(env_info_path)
    field_list = fields.split('.')
    info = env_info
    for field in field_list:
        info = info[field]
    return info


def get_ini_data(path):
    cfg = ConfigParser()
    cfg.read(path, encoding='utf-8')
    return dict(cfg.items())
# 读取ini文件
if __name__ == "__main__":
    print(get_ini_data('.\env.ini'))
    print(get_ini_data('.\env.ini')["env"])
    print(get_ini_data('.\env.ini')["env"]["flag"])
    print(get_ini_data('.\env.ini')["user"]["user1"])

# 读取yaml文件
if __name__ == "__main__":
    print(get_env_info('voiceCenter.headers-X20.deviceid','.\fangzhen_env.yaml'))

# 读取json文件

if __name__ == "__main__":
    data = get_json_data('.\test_query_schedule.json')
    print(data)
    print(data['ringtoneInfo'])
    print(data['ringtoneInfo']['ringtoneType'])

 

;