I am generating a configuration file for a service that expects a list of double quoted string options. I want to avoid installing additional packages via pip3 -r requirements.txt
as suggested in this answer and use the yaml
module that came available with python 3.8.10
on ubuntu 20.04
. I would like a way to solve this problem without searching for the lines and replacing them.
Python 3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>> yaml.__file__
'/usr/lib/python3/dist-packages/yaml/__init__.py'
python3 test.yaml
import yaml
configDict = {}
configDict["OptionList"] = [
"\"item1/Enable\"",
"\"item2/Disable\""
]
with open('./testConfig.yaml', 'w') as f:
yaml.dump(configDict, f)
testConfig.yaml
Current output:
OptionList:
- '"item1/Enable"'
- '"item2/Disable"'
Desired output:
OptionList:
- "item1/Enable"
- "item2/Disable"
It would need to digg in documentation and source code to see if it has option to change it.
At this moment I would simply get text
and use replace()
import yaml
configDict = {}
configDict["OptionList"] = [
'"item1/Enable"',
'"item2/Disable"'
]
text = yaml.dump(configDict)
print(text)
text = text.replace("'\"", '"').replace("\"'", '"')
print(text)
with open('./testConfig.yaml', 'w') as f:
f.write(text)
Result:
OptionList:
- '"item1/Enable"'
- '"item2/Disable"'
OptionList:
- "item1/Enable"
- "item2/Disable"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.