2

I get this error

UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f525' in position 0: character maps to

I would like to write for example "" to a txt file and it should be \U0001f525 written in the txt file

Here's my code

test1 = f"{config['emoji']}"


with open('emoji.txt', 'w') as f:
    f.write(test1)
Test123
  • 23
  • 4

1 Answers1

1
test1 = ""

with open('emoji.txt', 'w') as f:
    transformed = (test1
        .encode('utf-16', 'surrogatepass')\
        .decode('utf-16')\
        .encode("raw_unicode_escape")\
        .decode("latin_1"))
    f.write(transformed)

Adapted from this answer

bash721
  • 140
  • 2
  • 10