python 读写 yaml格式文件的 demo

关于 python 读写 yaml格式文件的 demo:

1. 创建 yaml

import os
import yaml
aa = {
	"A":"aaaaa",
	"B":"bbb",
	"C":"cc",
	"n":"2",
	"names":["a","b"]
	}
curpath = os.path.dirname(os.path.realpath(__file__))
yamlpath = os.path.join(curpath, "./yamlFile/test.yaml")
with open(yamlpath,"w",encoding='utf-8') as f :
	# yaml.dump({"projects": aa}, f)
	yaml.safe_dump({"projects": single_product}, f, default_style=False, allow_unicode=True)

2. 修改yaml

import os
import yaml

def set_data(status_num):
	file_name = "./yamlFile/test.yaml"
curpath = os.path.dirname(os.path.realpath(__file__))
yamlpath = os.path.join(curpath, "./yamlFile/test.yaml")
with open(file_name) as f :
	docum = yaml.safe_load(f)
	docum["n"] = status_num
	with open(file_neme, 'w') as f:
		yaml.safe_dump(docum, f, default_style=False, allow_unicode=True)


set_data(8)  # 将 1 中的  n = 2  修改 为 n = 8

以下是两个 较好的博客总结,分享一下:

  • python yaml用法详解:
  • https://blog.youkuaiyun.com/lmj19851117/article/details/78843486/
  • http://www.manongjc.com/detail/13-dvdngxmvlnhywqh.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值