安裝 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 存儲(chǔ)結(jié)構(gòu)如上,name +:+空格,后面可以是對(duì)象、字符串、bool值、整數(shù)、浮點(diǎn)數(shù)、時(shí)間、日期、null等
將數(shù)據(jù)寫入 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 文件數(shù)據(jù)
f = open('data.yaml', 'r', encoding='utf-8')
data = yaml.safe_load(f)
#獲取 user_name
user_name=data['user_name']
print(user_name)
打印結(jié)果
aa