安装 yaml
pip3 install PyYaml
yaml文件示例
data.yaml
user_name: 'aa'
pwd: '123456'
header: {'X-Requested-With': 'XMLHttpRequest'}
di: 4
stock: 1
buy_type: 'goods'
address_id: 2
payment_id: 1
spec: ''
user_note: ''
yaml 存储结构如上,name +:+空格,后面可以是对象、字符串、bool值、整数、浮点数、时间、日期、null等
将数据写入 yaml 文件中
import yaml
data = {'name': 'Lex',
'department': 'SQA',
'salary': 8000,
'annual leave entitlement': [5, 10]}
with open('write_yaml.yaml','w',encoding='utf-8') as f:
yaml.dump(data,f)
读取 yaml 文件数据
f = open('data.yaml', 'r', encoding='utf-8')
data = yaml.safe_load(f)
#获取 user_name
user_name=data['user_name']
print(user_name)
打印结果
aa