Reputation: 141
I have the following code
name = "testyaml"
version = "2.5"
os = "Linux"
sources = [
{
'source': 'news',
'target': 'industry'
},
{
'source': 'testing',
'target': 'computer'
}
]
And I want to make this yaml with python3
services:
name: name,
version: version,
operating_system: os,
sources:
-
source: news
target: industry
-
source: testing
target: computer
I need a help specially on Sources part that how I can add my dictionary list there
Upvotes: 1
Views: 292
Reputation: 12128
Python's yaml
module allows you to dump dictionary data into yaml format:
import yaml
# Create a dictionary with your data
tmp_data = dict(
services=dict(
name=name,
version=version,
os=os,
sources=sources
)
)
if __name__ == '__main__':
with open('my_yaml.yaml', 'w') as f:
yaml.dump(tmp_data, f)
Contents from the yaml:
services:
name: testyaml
os: Linux
sources:
- source: news
target: industry
- source: testing
target: computer
version: '2.5'
Upvotes: 0
Reputation: 4152
import yaml
name = "testyaml"
version = "2.5"
os = "Linux"
sources = [
{"source": "news", "target": "industry"},
{"source": "testing", "target": "computer"},
]
yaml.dump(
{"services": {"name": name, "version": version, "os": os, "sources": sources}}
)
Upvotes: 1