本次学习知识点了解 ini方法 格式要求:文件名结尾以:.ini 类似与键值对 key = value [section] option = value option = value [section] option = value option = value [section] option = value option = value 读取:单个读取 ConfigParse类 1.引入:from configparse import Configparse 2.实例化:conf = Configparse() 3.读取.ini文件:conf.read(fs,encoding="utf-8") 4.通过get方法,获取section下的option的值 1)value = get(section,option) # 字符串 2)boolean: getboolean() # 布尔值 3)int:getint() # 整数 4)float:getfloat() # 符点数 代码格式: from configparser import ConfigParser class HandleConfig(ConfigParser): def __init__(self,file_path): super().__init__() self.read(file_path, encoding="utf-8") conf = HandleConfig(file_path).yaml方法 from configparser import ConfigParser # 1、实例化 conf = ConfigParser() # 2、读取配置文件 conf.read("nmb.ini",encoding="utf-8") # 3、读取某一项配置值:get,全部都是字符串 value = conf.get("log","file_ok") print(value) print(type(value)) # 读取出来为布尔值 val = conf.getboolean("log","file_ok") print(val) # 获取当前的section # conf.sections() s = conf.options("log") print(s) # # 写入:set # conf.set("log","file_name","py303030.log") # # 写入文件 # conf.write(open("nmb.ini","w",encoding="utf-8")) yaml方法: 1.安装:pip install pyyaml 路径1:pycharm-seeting install package pyyaml 路径2:cmd下 pip install pyyaml 接受的格式:元组,字典 列表 格式:以.yaml结尾 - hello - world - key: hello value: world name: xiaojian log: name: py30 level: INFO file_ok: False file_name: py30.log mysql: user: nmb passwd: 123456 #mysql: # - xiaojian # - 女 import yaml 1.打开文件:open with open("nmb.yaml",encoding="utf-8") as fs: data = yaml.load(fs,yaml.FullLoader) print(data) 2.加载文件数据为字典或者列表对象 yaml.load(fs,loader=yaml.FullLoader) 代码格式: import yaml with open("nmb.yaml",encoding="utf-8") as fs: data = yaml.load(fs,yaml.FullLoader) print(data) for key,value in data.items(): print(key) print(value) 可以使用小技巧: 用断点方式:使用小计算器写代码调试查看.ini的格式:int/str/folate 用例一: user: python30 password1: 123456 password2: 123456 check: {"code": 1, "msg": "注册成功"} 用例二: user: python36 password1: 123456 password2: 123456 check: {"code": 0, "msg": "该账户已存在"} 用例三: user: password1: 123456 password2: check: {"code": 0, "msg": "所有参数不能为空"} 用例四: user: password1: password2: 123456 check: {"code": 0, "msg": "所有参数不能为空"} 用例五: user: python27 password1: password2: check: {"code": 0, "msg": "所有参数不能为空"} 用例六: user: password1: password2: check: {"code": 0, "msg": "所有参数不能为空"} 用例七: user: python26 password1: 123456 password2: 12345 check: {"code": 0, "msg": "两次密码不一致"} 用例八: user: python29 password1: 12345 password2: 12345 check: {"code": 0, "msg": "账号和密码必须在6-18位之间"} 用例九: user: python28 password1: abcdefghi1234567890 password2: abcdefghi1234567890 check: {"code": 0, "msg": "账号和密码必须在6-18位之间"}