Quotes at the beginning of a scalar have a special meaning in YAML, they make the difference between plain scalars with resp. double and single quoted scalars.
In order to dump strings that have the quotes "naturally", YAML has to go through extra hoops: inserting a backslash (\
) in case of double quotes, or an extra single quotes '
in case of single quoted strings. The latter is what causes your output.
If you don't need any more fine grained control, you can do:
import yaml
my_list = ["a", "b", "c"]
with open("my_yaml.yaml", "w") as f:
yaml.safe_dump(my_list, f, default_style='"', enable_unicode=True,
explicit_start=True, default_flow_style=False)
which gives:
---
- "a"
- "b"
- "c"
Please note that:
- you should always use
safe_dump()
(unless you can't), that way you know up-front that loading with safe_load()
(you are loading not using the unsafe load()
are you?) is going to work.
- PyYAML has a streaming interface: dump normally takes a second parameter, which if not specified makes it stream into a in-memory buffer and returning the content of that buffer, which you then stream to file. Don't do that it is memory-inefficient and slow.
The default_style='"'
will make any scalar into a string. Any scalar? Yes, any scalar, including keys for mappings (which actually might be what you want) and integers and all the other special types YAML supports:
Changing my_list
to:
my_list = ["a", "b", "c", dict(d=42), 3.14, True]
will give you:
---
- "a"
- "b"
- "c"
- "d": !!int "42"
- !!float "3.14"
- !!bool "true"
If you don't want that and only want (specific) sequence elements to be quoted, you can use ruamel.yaml
(disclaimer: I am the author of that package), and do something like:
import ruamel.yaml
from ruamel.yaml.scalarstring import (DoubleQuotedScalarString as dq,
SingleQuotedScalarString as sq)
yaml = ruamel.yaml.YAML()
my_list = [dq("a"), sq("b"), dq("c"), dict(d=42), 3.14, True]
with open("my_yaml.yaml", "w") as f:
yaml.dump(my_list, f)
to get:
- "a"
- 'b'
- "c"
- d: 42
- 3.14
- true