How to remove single quotes from string inside curly braces? (python, yaml)

Viewed 35

I'm using pyyaml. And writing code that will convert user information into yaml file of certain style. And I have certain requirements. Like some strings should have single qoutation, some double quotation and the rest should have no quotation mark at all, but they do.

I want achieve the following output in the yaml file:

class: {child}

But I get this:

class: '{child}'

How can I get rid of single quotes here? And it does same for 'false'/'true'. It keeps quotes.

I am using python 3.9,PyYAML 6.0

, thanks in advance.

*Some updates. What I used to do is:

data = {}
data.update({'dev_class' : '{child}'})

And dump it

with open("package.yaml", 'w', encoding='utf-8') as f:
    yaml.dump(data,f, allow_unicode=True,encoding='utf-8',sort_keys=False, width=float("inf"))

That will result as I already mentioned:

dev_class: '{child}'

I tried to manipulate with string itself using str(), replace(), and so on. It had no result. Then since I am kinda declaring a set, I decide to add not a string, but a set.

data.update({'dev_class' : {'moisture'}})

And I received the following:

dev_class: !!set
o:
e:
t:
m:
i:
s:
u:
r:

So, I still in search.

1 Answers
s = 'class: \'{child}\''

print(s.replace('\'', ''))

Edit1:

import yaml

data = {}
data.update({'dev_class' : '{child}'})

str_data = ""
for i in data:
    str_data += i + ": " + data[i]

with open("package.yaml", 'w', encoding='utf-8') as f:
    yaml.dump(str_data,f, allow_unicode=True,encoding='utf-8',sort_keys=True, width=float("inf"))
Related