Problem with encoding

There is a list of data in Ukrainian, from which I randomly select two values

Then I create a file, open it and write down the data that I randomly got

But in the resulting file I do not have the values ​​in Ukrainian, but the characters, for example: "\ u0422 \ u0443 \ u0445 \ u043b \ u044f" The code itself:

from model.list_ukr_lang import list_22 import random import os.path import json testdata = [ Group(name1=random.choice(list_22), name2=random.choice(list_22)) ] file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../data/groups.json") with open(file, "w") as f: f.write(json.dumps(testdata, default=lambda x: x.__dict__, indent=2)) 

Can you please tell me how to get data in Ukrainian?

    1 answer 1

    Try this:

     with open(file, "w", encoding='utf-8') as f: json.dump(testdata, f, indent=2, ensure_ascii=False) 
    • Super, helped, thanks! but I tried a bunch of ways, except for "ensure_ascii = False" - Andrei Petrov