更新时间:2022-06-20 07:46:42
在您的with
块中,您可以将所需的任何内容写入文件.由于您只需要在顶部添加评论,因此在调用ruamel之前先添加f.write()
呼叫:
Within your with
block, you can write anything you want to the file. Since you just need a comment at the top, add a call to f.write()
before you call ruamel:
with open('test.yml', "w") as f:
f.write('# Data for Class A\n')
ruamel.yaml.dump(
d, f, Dumper=ruamel.yaml.RoundTripDumper,
default_flow_style=False, width=50, indent=8)