新建个xx.yaml文件,写入如下格式数据
username: "wyatt"
password: 123
database: 127.1.23.32
# 字典1
info1: {"aa": "123", "bb": "asd"}
# 注释使用#
# 列表1
hobby1: [1, 2, 3]
# 下面这种写法 = info: {"aa": "123", "bb": "asd"}
# 字典2
info2:
aa: '123'
bb: 'asd'
读取方式:
""" 读取yaml文件: 1.安装pyyaml模块 2.import yaml """ import yaml # 读取yaml文件 with open('demo1.yaml', encoding='utf-8') as f: # 安全加载 data = yaml.safe_load(f) # data 是一个字典 print(data)
新建个xx.ini文件,写入如下格式数据
[default]
host=http://www.a.com
[db]
host="<http://www.a.com>"
port=3369
name="py"
读取ini的方式:
from configparser import ConfigParser parser = ConfigParser() parser.read("demo2.ini", encoding="utf-8") host = parser.get("default", "host") db_host = parser.get("db", "host") db_port = parser.get("db", "port") print(host) print(db_host) print(type(db_port)) # 打印出来的都是字符串