Reputation: 1861
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 ?
Upvotes: 4
Views: 728
Reputation: 39638
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:
- ⬆️😢
Upvotes: 6