如何在 Python 中将折叠标量转储到 YAML(使用 ruamel?) [英] How to dump a folded scalar to YAML in Python (using ruamel?)
问题描述
我一直在搜索 stackoverflow,寻找一种使用 Python 以 YAML 格式转储折叠标量的方法.常见的答案来自用户Anthon 建议使用他的 ruamel Python 库.我接受了建议,但我不知道如何以折叠样式转储长 Python 字符串值.
I've been scouring stackoverflow looking for a way to dump a folded scalar in YAML format using Python. A common answer is from user Anthon who suggests using his ruamel Python library. I took the advice but I cannot figure out how to dump a long Python string value in folded style.
在 Anthon 的回答中,他/她经常使用带有折叠样式代表>"的硬编码字符串来说明他的观点,如下所示:
In Anthon's answer's he/she often uses a hard-coded string with the folded style representer ">" to illustrate his point like so:
yaml_str = """\
long: >
Line1
Line2
Line3
"""
data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)
print(yaml.dump(data, Dumper=yaml.RoundTripDumper))
我不确定如何将该示例翻译成我自己的代码,其中我想转储的字符串值不是来自已包含折叠表示的硬编码值,而是来自 Django 请求(好吧它真的可以来自任何地方,重点是,我没有在我的代码中使用>"手动构建字符串).
I'm not sure how to translate that example into my own code where the string value I'd like to dump comes not from a hard-coded value with the folded representer already in it, but from a Django request (well it could come from anywhere really, the point is, I'm not constructing the string in my code manually with ">").
我真的想做这样的事情吗:
Am I really meant to do something like:
stringToDumpFolded = "ljasdfl\n\nksajdf\r\n;lak'''sjf"
data = "Key: > \n" + stringToDumpFolded
ruamel.yaml.dump(data, f, Dumper=yaml.RoundTripDumper))
否则,给定一个很长的 unicode 字符串变量,我如何使用 ruamel 将其转储到文件中?
Otherwise, given a long unicode string variable, how do I use ruamel to dump it to a file?
推荐答案
从 0.15.61 开始,可以在 ruamel.yaml 中往返折叠标量:
Starting with 0.15.61 it is possible to round-trip folded scalars in ruamel.yaml:
import sys
import ruamel.yaml
yaml_str = """\
long: >
Line1
Line2
Line3
"""
yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
print(type(data['long']), data['long'].fold_pos, end='\n-----\n')
yaml.dump(data, sys.stdout)
给出:
<class 'ruamel.yaml.scalarstring.FoldedScalarString'> [5, 11]
-----
long: >
Line1
Line2
Line3
该类型的印刷品仅用于显示您如何建立如果从头开始工作,你自己要创建什么对象:
The print of the type is only there to show how you could establish yourself what object to create if working from scratch:
from ruamel.yaml.scalarstring import FoldedScalarString as folded
s = folded('Line1 Line2 Line3\n')
data = dict(long=s)
yaml.dump(data, sys.stdout)
给出折叠标量,但可能不是您想要的方式:
which gives a folded scalar, but probably not the way you want:
long: >
Line1 Line2 Line3
要使其折叠,您必须提供 .fold_pos
属性.那属性必须是一个列表(或一些可逆的可迭代对象),其中 字符串中的空格字符,需要插入折叠的地方:
To get this to fold you have to provide the .fold_pos
attribute. That
attribute has to be a list (or some reversable iterable) with the position of
space characters in the string, where folds need to be inserted:
s = folded('Line1 Line2 Line3\n')
s.fold_pos = [5, 11]
data = dict(long=s)
yaml.dump(data, sys.stdout)
返回您期望的输出:
long: >
Line1
Line2
Line3
由于您似乎希望折叠所有空间,您还可以执行以下操作:
Since you seem to want all spaces to be folded, you can also do something like:
import re
s.fold_pos = [x.start() for x in re.finditer(' ', s)]
这篇关于如何在 Python 中将折叠标量转储到 YAML(使用 ruamel?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!