-1

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.

Unknown _
  • 17
  • 3

1 Answers1

0
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"))
  • When I dumping some information into yaml file using python, I am using dictionary. So, basically it looks like this: `data = {}` `data.update({'class' : '{child}'})` I've tried to do the way you told `data.update({'class' : '\'{child}\''.replace('\'','')})` but the result was the same. – Unknown _ Sep 15 '22 at 08:32
  • @Unknown_ Try it right now, so you need to put dict data in str type! – Mohamad Ghaith Alzin Sep 16 '22 at 11:01
  • No...it raises TypeError. You sre not allowed to concatenate dict with str. Only str with str. But anyway, thanks for help. – Unknown _ Sep 20 '22 at 08:23