【发布时间】:2021-06-26 23:04:19
【问题描述】:
我正在将一个相当大的字典转储到 YAML 文件中。该文件没有扩展,而是将有序字典作为值
import ruamel.yaml as yaml
import json
with open(file) as file:
data = yaml.load(file, yaml.RoundTripLoader)
data = json.loads(json.dumps(data))
data.update(other_dict)
with open("file_new.yaml", "w") as file:
yaml.dump(data, file, default_flow_style=False, Dumper=yaml.RoundTripDumper)
期望的输出
Hello:
Hello:
Hello: Hi there
Hello2: Hi
实际输出
Hello:
Hello: ordereddict(["Hello": "Hi there", "Hello2": "Hi"])
【问题讨论】:
-
请提供minimal, reproducible example。您的代码引用了未知变量(
file、other_dict)和未知文件。如果您在代码中以字符串形式提供原始 YAML 内容并将输出写入stdout,其他人更容易重现您的问题。
标签: python dictionary yaml ordereddictionary ruamel.yaml