美文网首页
用yaml格式的文件做配置文件

用yaml格式的文件做配置文件

作者: 昵称违法 | 来源:发表于2019-11-06 16:32 被阅读0次

    yaml的知识,自行百度。
    本文涉及:yaml文件制作,读取、修改、保存

    配置文件内容:C:/缓存数据/winCelue03.yaml

    date_start_list_v:            #注释说明
        - '2016-01-01'
        - '2017-01-01'
        - '2018-01-01'
        - '2019-01-01'
        - '2016-01-01'      
    date_end_list_v:             #注释说明
        - '2016-12-31'
        - '2017-12-31'
        - '2018-12-31'
        - '2019-12-31'
        - '2019-12-31'
    plan_v:                           #注释说明
        'a'                                   
    isDisplay_v:                    #注释说明
        False                            
    money_begin_v:            #注释说明   
        10000000                        
    percent_kc_v :                #注释说明
        '1/4'
    qihuoList_v:                    #注释说明
        - '甲醇'
        - '焦炭'
        - '塑料'
        - 'IF300'
        - '螺纹钢'
        - '棕榈油'
        - '铜'
    config_file_path_v:          #注释说明
        'E:/data/跳空缺口/配置文件/涨停策略模板.xls'
    factor_file_path_v:           #注释说明
        'E:/data/跳空缺口/配置文件/分布系数.xls'
    daPan_file_path_v:           #注释说明  
        'E:/data/qihuo/shangzhengzhishu/上证指数.xls'
    result_save_directory_v:   #注释说明
        'C:/缓存数据'    
    kline_directory_v:             #注释说明
        'E:/data/跳空缺口/14个商品期货主力连续合约K线'
    

    读取配置文件:

    #yaml配置文件.yaml
    import sys
    import yaml
    
    #读取yaml格式的配置文件
    def read_yaml_config(file_path):
        """
        file_path:文件路径及名字
        """
        f = open(file_path,encoding='utf-8')
        config = yaml.load(f,Loader=yaml.FullLoader)
        print(f.close())
        return config
    
    file_path = 'C:/缓存数据/winCelue03.yaml'
    content = read_yaml_config(file_path)
    for k,v in content.items():
        print(f"type of k is {type(k)}  type of v is {type(v)}")
        print(k,v)
    

    读取的配置文件为:

    None
    type of k is <class 'str'>  type of v is <class 'str'>
    config_file_path_v E:/data/跳空缺口/配置文件/涨停策略模板.xls
    type of k is <class 'str'>  type of v is <class 'str'>
    daPan_file_path_v E:/data/qihuo/shangzhengzhishu/上证指数.xls
    type of k is <class 'str'>  type of v is <class 'list'>
    date_end_list_v ['2016-12-31', '2017-12-31', '2018-12-31', '2019-12-31', '2019-12-31']
    type of k is <class 'str'>  type of v is <class 'list'>
    date_start_list_v ['2016-01-01', '2017-01-01', '2018-01-01', '2019-01-01', '2016-01-01']
    type of k is <class 'str'>  type of v is <class 'str'>
    factor_file_path_v E:/data/跳空缺口/配置文件/分布系数.xls
    type of k is <class 'str'>  type of v is <class 'bool'>
    isDisplay_v False
    type of k is <class 'str'>  type of v is <class 'str'>
    kline_directory_v E:/data/跳空缺口/14个商品期货主力连续合约K线
    type of k is <class 'str'>  type of v is <class 'int'>
    money_begin_v 10000000
    type of k is <class 'str'>  type of v is <class 'str'>
    percent_kc_v 1/4
    type of k is <class 'str'>  type of v is <class 'str'>
    plan_v a
    type of k is <class 'str'>  type of v is <class 'list'>
    qihuoList_v ['甲醇', '焦炭', '塑料', 'IF300', '螺纹钢', '棕榈油', '铜']
    type of k is <class 'str'>  type of v is <class 'str'>
    result_save_directory_v C:/缓存数据
    type of k is <class 'str'>  type of v is <class 'int'>
    

    修改配置文件:

    content['年龄'] = 119  #增加【年龄】内容
    

    保存配置文件:

    #保存yaml文件
    def save_yaml_config(file_path,yaml_config):
        """
        file_path:文件路径及文件名
        yaml_config:yaml内容{dict type}
        """
        f = open(file_path,'w')
        yaml.dump(yaml_config,f)
        f.close()
    
    save_yaml_config(file_path,content)  
    

    读取并查看

    content = read_yaml_config(file_path)
    for k,v in content.items():
        print(f"type of k is {type(k)}  type of v is {type(v)}")
        print(k,v)
    

    读取的内容:

    None
    type of k is <class 'str'>  type of v is <class 'str'>
    config_file_path_v E:/data/跳空缺口/配置文件/涨停策略模板.xls
    type of k is <class 'str'>  type of v is <class 'str'>
    daPan_file_path_v E:/data/qihuo/shangzhengzhishu/上证指数.xls
    type of k is <class 'str'>  type of v is <class 'list'>
    date_end_list_v ['2016-12-31', '2017-12-31', '2018-12-31', '2019-12-31', '2019-12-31']
    type of k is <class 'str'>  type of v is <class 'list'>
    date_start_list_v ['2016-01-01', '2017-01-01', '2018-01-01', '2019-01-01', '2016-01-01']
    type of k is <class 'str'>  type of v is <class 'str'>
    factor_file_path_v E:/data/跳空缺口/配置文件/分布系数.xls
    type of k is <class 'str'>  type of v is <class 'bool'>
    isDisplay_v False
    type of k is <class 'str'>  type of v is <class 'str'>
    kline_directory_v E:/data/跳空缺口/14个商品期货主力连续合约K线
    type of k is <class 'str'>  type of v is <class 'int'>
    money_begin_v 10000000
    type of k is <class 'str'>  type of v is <class 'str'>
    percent_kc_v 1/4
    type of k is <class 'str'>  type of v is <class 'str'>
    plan_v a
    type of k is <class 'str'>  type of v is <class 'list'>
    qihuoList_v ['甲醇', '焦炭', '塑料', 'IF300', '螺纹钢', '棕榈油', '铜']
    type of k is <class 'str'>  type of v is <class 'str'>
    result_save_directory_v C:/缓存数据
    type of k is <class 'str'>  type of v is <class 'int'>
    年龄 119
    

    相关文章

      网友评论

          本文标题:用yaml格式的文件做配置文件

          本文链接:https://www.haomeiwen.com/subject/gunubctx.html