2

I am using python 3.9.5, and PyYaml 5.4.1.

I have a file t.yml with the following content.

- ⬆️

I have written a simple python code, to read the yaml file and dump it back.

import yaml

with open("t.yml") as file:
    con = yaml.safe_load(file)
    print(con)
    with open("t.yml","w") as file:
        yaml.dump(con,file)

The output of the code is:

['⬆️']

After dumping the yaml, the t.yml file becomes like this:

- "\u2B06\uFE0F\U0001F622"

How can I dump the emojis in the exact same format, I loaded them ?

aahnik
  • 1,661
  • 1
  • 11
  • 29

1 Answers1

3

Generally, YAML loses information when loading a file (see this question) so you cannot always dump it exactly the way it is written, since the information on how it was written has been lost.

In this case the solution is to set allow_unicode:

import sys,yaml

input = """
- ⬆️
"""

con = yaml.safe_load(input)
yaml.dump(con,sys.stdout, allow_unicode=True)

Output:

- ⬆️
flyx
  • 35,506
  • 7
  • 89
  • 126
  • for some reason google collab notebooks fail here with `ReaderError: unacceptable character #x1f622: special characters are not allowed in "", position 5` https://imgur.com/a/c7S1fGN – andilabs Jul 31 '22 at 12:34
  • 1
    @andilabs That might be an IPython problem but I'm no expert on that. I suggest asking a new question if you want an answer from someone knowledgable. – flyx Jul 31 '22 at 12:46