Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python yaml dump emojis as is

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 ?

like image 378
aahnik Avatar asked Sep 18 '25 03:09

aahnik


1 Answers

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:

- ⬆️😢
like image 55
flyx Avatar answered Sep 20 '25 18:09

flyx