Having Issues Escaping Single Quote
Solution 1:
I'm not sure if I fully understood what you are asking but you just inserted 3 string for the value and concatenated them. In
new_value.insert(len(new_value), '456','''no'' ', comment="New Value")
The '' 'no'' '
part translates to ''=<Empty String> 'no'=no ' '=<Space>
.
So if you want a regurlar 'no'
just do:
new_value.insert(len(new_value), '456','no', comment="New Value")
Solution 2:
If you would try:
print(repr('''no'' '))
it will give you:
'no '
as what you do is juxtapose the string ''
with the string 'no'
and
the string ' '
, which Python just concatenates to 'no '
before calling print
. What you
are trying to do is use YAML single quotes scalar escaping mechanism
in Python, and that would only work if you YAML().load()
the string from YAML.
If you want a single quoted scalar in your output, just like you have
in your input, you should make scalar the same type as what you get
using .load()
. That is
ruamel.yaml.scalarstring.SingleQuotedScalarString
(as string
subclass).
import sys
from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import SingleQuotedScalarString as sq
yaml_doc = """\
Mappings:
Values:
'123': 'no'
"""
yaml = YAML()
yaml.preserve_quotes = True
data = yaml.load(yaml_doc)
new_value = data['Mappings']['Values']
new_value['456'] = sq('no')
new_value.yaml_add_eol_comment(comment="New Value", key='456', column=0)
new_value.insert(len(new_value), '789', sq('no'), comment="New Value 2")
yaml.dump(data, sys.stdout)
this gives:
Mappings:
Values:
'123': 'no''456': 'no' # NewValue'789': 'no' # NewValue2
Solution 3:
UPDATE: I was informed by @Anthon that earlier versions of YAML would treat no and 'no' as different values, considering no to be a boolean value. So please only take what I say below as applying to the current version of YAML. Also, maybe there's another motivation here beyond writing YAML that will produce a particular result from an up to date parser. In that case, you have a good answer above from @Anthon as to how to force those quotes into the resulting YAML via a writer that can't take the older YAML format into account (I'd suggest making sure that there is no such mode in the writer).
YAML is about conveying a particular data structure, with dictionaries, lists and values. When YAML is parsed, the resulting data structure is all you care about. If two YAML files are different but produce the same data structure, the differences shouldn't matter to you.
Here, I'm not sure what the OP wants. It appears that he either wants the word no to end up a few times in the data structure that his YAML represents, or maybe he wants 'no' to be the value that ends up in the data structure. Here are the most concise ways of writing each of those (in YAML 1.2):
no ==
no
'no' ==
"'no'"
There is no case here where the characters 'no' need to show up in the YAML in the way the OP is trying to force. All of this extra code is trying to force those characters into the YAML when there's no need to do so. That construct means the same thing as no
. Just forget about the quotes.
Post a Comment for "Having Issues Escaping Single Quote"