--- # develop(键值对,值默认为字符串)
basic:
# 地址参数
request_url: ""
get_userinfo: ""
topcardweb_url: ""
swagger_url: ""
# 账户密码
mortgage_username: ""
mortgage_password: ""
# 进件参数
ProvinceCode: ""
CityCode: ""
AreaCode: ""
Address: ""
productId: ""
merchantId: ""
storeId: ""
custMgrId: ""
loanPeriod: ""
sfz_filepicture_dict1:
{
"fileKind": "1",
# "fileUrl": "http://, # 人像
"fileUrl": "http://", # 人像
"fileName": "身份证正面"
}
sfz_filepicture_dict2:
{
"fileKind": "2",
# "fileUrl": "http://",
"fileUrl": "http://",
"fileName": "身份证背面"
}
# 合同参数
# preCreditApplySave_compactId: ""
# preCreditApplySave_compactType: ""
# commitPreCreditInfo_compactId: ""
# commitPreCreditInfo_compactType: ""
queryCompactDetail_compactId: ""
queryCompactDetail_compactType: ""
commitLoanApply_compactId: ""
commitLoanApply_compactType: ""
reSignContractList_compactId: ""
reSignContractList_compactType: ""
affiliatedAuthContract_compactId: ""
affiliatedAuthContract_compactType: ""
# compactId: "4387318610540437635"
# compactType: "90"
database_info:
database_name: "mysql"
database_prefix: ""
host: ""
username: ""
password: ""
port: ""
database_differences: ""
#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2020/5/4 and 10:24
Project:Python3
FileName:comment_txt.py
Description:
1.yaml [ˈjæməl]: Yet Another Markup Language :另一种标记语言。
yaml 是专门用来写配置文件的语言,非常简洁和强大,之前用ini也能写配置文件,看了yaml后,发现这个更直观,更方便,有点类似于json格式
2.yaml基本语法规则:
大小写敏感
使用缩进表示层级关系
缩进时不允许使用Tab键,只允许使用空格。
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
#表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的注释一样
3.yaml支持的数据结构有三种:
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值。字符串、布尔值、整数、浮点数、Null、时间、日期
4.多个文档在一个yaml文件,使用 --- 分隔方式来分段
load_all读取
'''
class Yaml_config():
def yaml_write(self, data=None, mode='a+', yamlPath="D:\Mytest\Python3\Python3\...\...\config\config.yaml"):
"""
@param data: 写入的数据(字典、列表、字符串)
@param yamlPath: 生成文件的路径
@return: f.read()对象(dict)
"""
import yaml
with open(yamlPath, mode=mode, encoding='utf-8') as file_write, open(yamlPath, 'r', encoding='utf-8') as file_read:
yaml.dump(data, file_write) # 装载数据
read_all = file_read.read() # 读取文件
load_all = yaml.safe_load(read_all) # 加载数据
print(f"----写入配置文件:{type(load_all)}----\n{load_all}\n") # 打印数据
return load_all
def yaml_read(self, readnum="load_all", yamlPath="D:\Mytest\Python3\Python3\...\...\config\config.yaml"):
import yaml
f = open(yamlPath, 'r', encoding='utf-8')
if readnum == "load_one":
read_all = f.read().replace("---", "").replace("'", '''"''') # 读取文件
load_one = yaml.safe_load(read_all)
# print(f"----读出配置文件:{type(load_one)}\n{load_one}") # 打印数据
return load_one
elif readnum == "load_all":
read_all = f.read() # 读取文件
load_all = [i for i in yaml.safe_load_all(read_all)]
# print(f"----读出配置文件:{type(load_all)}\n{load_all}") # 打印数据
return load_all
def write_configparser(self, yamlPath="D:\Mytest\Python3\Python3\...\...sit\config\\test.ini"):
import configparser
config = configparser.ConfigParser()
config.add_section("AXIS_0")
config.set("AXIS_0", "time", "500")
config.set("AXIS_0", "acc", "10")
config.add_section("AXIS_1")
config.set("AXIS_1", "time", "250")
config.write(open(yamlPath, "w"))
if __name__ == '__main__':
# 写入
# dev_database = {"dev_database_info": {"host": "", "port": "", "database_name": "", "database_prefix": "", "username": "", "password": ""}}
# Yaml().yaml_write(data=dev_database, mode='a+')
# # # 读出
# load_one = Yaml().yaml_read(readnum="load_one")
cofing_load_all = Yaml_config().yaml_read(readnum="load_all")
for i in cofing_load_all:
print(type(i), i)
cofing_dict = cofing_load_all[2]
# get_basic = cofing_load_all[2]['basic']['request_url']
# get_database = cofing_load_all[2]['database_info']['database_name']
get_dev = cofing_dict['database_info']['database_prefix']
get_basic = cofing_dict['basic']['sfz_filepicture_dict1']["fileUrl"]
get_database = cofing_dict['database_info']['database_differences']
database = {
"host": cofing_dict['database_info']['host'],
"port": cofing_dict['database_info']['port'],
"database_name": cofing_dict['database_info']['database_name'],
"database_prefix": cofing_dict['database_info']['database_prefix'],
"username": cofing_dict['database_info']['username'],
"password": cofing_dict['database_info']['password'],
"database_differences": cofing_dict['database_info']['database_differences'],
"database_differences1": cofing_dict['basic']['sfz_filepicture_dict1']["fileUrl"]
}
print(type(database), database)
# print(type(get_basic), get_basic)
# print(type(load_all[cofing_dict['database_info']['database_prefix'] + "_" + "database_info']['database_name']), load_all[cofing_dict['database_info']['database_prefix'] + "_" + "database_info']['database_name'])
# print(type(load_all['timeout']), load_all['timeout'])