简体   繁体   中英

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 ?

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:

- ⬆️😢

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM