I have a my_yaml.yml
file with the following content:
my_yaml:
person: >
John|Doe|48,
Jack|Black|39
skills:
- name: superhero
abilities:
- swim
- run
special_chars:
- '! | " "'
- '+ | " "'
- '\ | " "'
- 'Á | "A"'
- 'É | "E"'
- 'Ű | "U"'
- 'Û | "U"'
I want to load it then dump into a my_yaml_new.yml
file having totally the same format & characters as the original input file has. My code is:
import yaml
my_yaml = yaml.load(open('my_yaml.yml', encoding='utf8')) # without "utf8" encoding I get "'charmap' codec can't decode byte..." error
I can dump
it into console but 1) order of abilities
& name
has changed :(
yaml.dump(my_yaml, default_flow_style=False, allow_unicode=True)
Result is:
'my_yaml:\n person: >\n John|Doe|48, Jack|Black|39\n skills:\n - abilities:\n - swim\n - run\n name: superhero\n special_chars:\n - \'! | " "\'\n - + | " "\n - \\ | " "\n - Á | "A"\n - É | "E"\n - Ű | "U"\n - Û | "U"\n'
And when I try to dump into a file:
with open('my_yaml_new.yml', 'w') as outfile:
yaml.dump(my_yaml, outfile, default_flow_style=False, allow_unicode=True)
2) I get the following error due to character Û
:
UnicodeEncodeError: 'charmap' codec can't encode character '\xdb' in position 0: character maps to undefined
If I delete this line from the input my_yaml.yml
file then above dump is successful, but 3) my multiple lines at person
string go into one line :(
my_yaml:
person: >
John|Doe|48, Jack|Black|39
skills:
- abilities:
- swim
- run
name: superhero
special_chars:
- '! | " "'
- + | " "
- \ | " "
- Á | "A"
- É | "E"
- Ű | "U"
4) And also my single quotes (') are disappeared from special_chars
:(
5) And also note that elements of skills
has no indentation :(
I've tried these solutions with no success. And nor import ruamel.yaml as yaml
has helped.
UPDATE
OK, the following great package solves problems 1) & 4), and I can replace >
to |
at multi line values so 3) is also solved. And maybe 5) is not a huge problem. But I still struggle with special characters like Û
or Ǘ
so I'm still looking for solution for problem 2)...
from ruamel import yaml
my_yaml = yaml.round_trip_load(open('dmy_yaml.yml', encoding='utf8'), preserve_quotes=True)
with open('my_yaml_new.yml', 'w') as outfile:
yaml.round_trip_dump(my_yaml, outfile, default_flow_style=False, allow_unicode=True)