简体   繁体   English

如何使用带有长键和值的字典编写YAML文件?

[英]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=FalseTrue ,使用no-flow看起来更好,但这不是我想要的。

您可以通过将width参数传递给dump方法来限制行的width

yaml.dump(d,  width=20, default_flow_style=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python的yaml文件中配置多个字典键? - How do I configure multiple dictionary keys in a yaml file in Python? 如何使用python从字典中写入Yaml文件? - How do I write a yaml file from a dictionary with python? 如何使用字典创建CSV文件,并且仅将变量用作键和值 - How do I create a CSV file with a dictionary and only using variables as keys and values 如何与字典中的值交换键? - How do I exchange keys with values in a dictionary? 如何使用来自其他字典的键和来自 Python 中的 csv 文件的值制作字典? - How do I make a dictionary with keys from a other dictionary and values from a csv file in Python? 将excel文件转换成字典后,如何为不在字典中的键和值返回“ none”? - After turning an excel file into a dictionary, how do I return “none” for the keys and values not in the dictionary? 如何将字典键和相应的值写入文件的每一行 - How to write dictionary keys and corresponding values to each line to a file 如何使用 for 循环将整个 csv 文件写入字典 - How do I write the whole csv file into a dictionary using for loops 使用字典,如何在对键进行排序后将“值”和“键”保持在一起? - Using a dictionary, how do I keep Values and Keys together after sorting the keys? 将字典(键和值)写入csv文件 - Write dictionary (keys and values) to a csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM