使用时如何防止新行\字符输出到YAMLruamel.yaml.scalarstring公司.DoubleQuotedScalarString?

2024-09-30 20:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用鲁阿迈尔.亚马尔. 我需要显式引用一些字符串,以便将它们传递给ruamel.yaml.scalarstring公司.DoubleQuotedScalarString,它在输出中将它们用双引号括起来。但是,覆盖多行的字符串在输出中包含\字符。如何防止这种情况?你知道吗

在将字符串传递给ruamel.yaml.scalarstring公司.DoubleQuotedScalarString并将其替换为“”,但这不起作用。我认为这些字符是在转储时添加的。你知道吗

S = ruamel.yaml.scalarstring.DoubleQuotedScalarString
string = "The Parkinson's Disease Sleep Scale-Validation (PDSS-2) is a scale used to characterise and quantify the various aspects of nocturnal sleep problems in Parkinson's disease (PD). The PDSS-2 is a revised version of the Parkinson's Disease Sleep Scale (PDSS)."
st = S(string)
data = {'key': st}

f = open('./yam.yaml', 'w')
yaml= YAML()
yaml.default_flow_style = False
yaml.indent(offset = 2, sequence = 4, mapping = 2)
yaml.dump(data, f)

期望值:

key: "The Parkinson's Disease Sleep Scale-Validation (PDSS-2) is a scale used to characterise and quantify the various aspects of nocturnal sleep problems in Parkinson's disease (PD). The PDSS-2 is a revised version of the Parkinson's Disease Sleep Scale (PDSS)."

实际值:

key: "The Parkinson's Disease Sleep Scale-Validation (PDSS-2) is a scale used to characterise\
  \ and quantify the various aspects of nocturnal sleep problems in Parkinson's disease\
  \ (PD). The PDSS-2 is a revised version of the Parkinson's Disease Sleep Scale (PDSS)."

Tags: ofthe字符串yamlisruamelsleepvalidation
1条回答
网友
1楼 · 发布于 2024-09-30 20:38:38

您看到的是由换行引起的,它试图将输出中的行长度限制为默认的80个字符。你知道吗

如果您可以使用更广泛的输出,那么只需将yaml.width设置为更大的值:

import sys
import ruamel.yaml
YAML = ruamel.yaml.YAML

S = ruamel.yaml.scalarstring.DoubleQuotedScalarString
string = "The Parkinson's Disease Sleep Scale-Validation (PDSS-2) is a scale used to characterise and quantify the various aspects of nocturnal sleep problems in Parkinson's disease (PD). The PDSS-2 is a revised version of the Parkinson's Disease Sleep Scale (PDSS)."
st = S(string)
data = {'key': st}

yaml= YAML()
yaml.width = 2048
yaml.default_flow_style = False
yaml.indent(offset = 2, sequence = 4, mapping = 2)
yaml.dump(data, sys.stdout)

它给出:

key: "The Parkinson's Disease Sleep Scale-Validation (PDSS-2) is a scale used to characterise and quantify the various aspects of nocturnal sleep problems in Parkinson's disease (PD). The PDSS-2 is a revised version of the Parkinson's Disease Sleep Scale (PDSS)."

相关问题 更多 >