I have the following YAML file:
---
my_vars:
my_env: "dev"
my_count: 3
When I read it with PyYAML and dump it again, I get the following output:
---
my_vars:
my_env: dev
my_count: 3
The code in question:
with open(env_file) as f:
env_dict = yaml.load(f)
print(yaml.dump(env_dict, indent=4, default_flow_style=False, explicit_start=True))
I tried using the default_style
parameter:
with open(env_file) as f:
env_dict = yaml.load(f)
print(yaml.dump(env_dict, indent=4, default_flow_style=False, explicit_start=True, default_style='"'))
But now I get:
---
"my_vars":
"my_env": "dev"
"my_count": !!int "3"
What do I need to do to keep the original formatting, without making any assumptions about the variable names in the YAML file?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…