[英]How do I write a YAML file using a dictionary with long keys and values?
I have a Python dictionary that has 25-30 key-value pairs. 我有一本具有25-30个键值对的Python字典。 The keys are fairly long strings (20-30 characters), and the values can be 1-10 lines long with embedded new-lines, etc. 键是相当长的字符串(20-30个字符),值可以是1-10行,带有嵌入式换行符,等等。
I would like to write this to a file using YAML, but if I use yaml.dump
it is not very readable, because of the length of the data. 我想使用YAML将其写入文件,但是如果使用yaml.dump
,由于数据的长度,它的可读性不高。 I would really like the file to look like the following example: 我真的希望文件看起来像以下示例:
This_is_my_very_long_key_1 : "This is some value for key 1"
This_is_a_shorter_key : "This is the value
for that key"
I am finding the YAML documentation fairly sparse. 我发现YAML文档相当稀疏。
I tried using default_flow_style=False
or True
, and it looked better with no-flow
, but that's not what I wanted. 我尝试使用default_flow_style=False
或True
,使用no-flow
看起来更好,但这不是我想要的。
您可以通过将width
参数传递给dump方法来限制行的width
。
yaml.dump(d, width=20, default_flow_style=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.