当我们费尽心力寻找各种软件试图打开.yaml文件时,却忘记了作为一名程序猿的基本功,编代码的能力。
打开.yaml文件的代码如下:
def load_config(config_file):
with open(config_file, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
return config
config_file = "configs/config_{}.yaml".format("birds")
config = load_config(config_file)
print(config)
yaml文件的信息就输出出来了!
那我们怎么修改呢?
代码如下:
import yaml
def set_state(state):
with open("config_birds.yaml") as f:
doc=yaml.safe_load(f)
doc['num_class'] = state
with open("config_birds.yaml", 'w') as f:
yaml.safe_dump(doc, f, default_flow_style=False)
set_state(216)
结果:
可看到"num_class"这个标签的值变成了216!