E
E
Enot_Star2020-05-13 22:23:47
Python
Enot_Star, 2020-05-13 22:23:47

How to work with Cyrillic in JSON?

I have a class:

spoiler

import json
import atexit

class JSONController:
    def __init__(self, filename):
        self.handler = open(filename, 'r+', encoding='utf-8')
        atexit.register(self.close)

    def load(self):
        text = self.handler.read()
        return json.loads(text)

    def save(self, data):
        text = json.dumps(data, ensure_ascii=True, indent=4)
        self.handler.seek(0)
        self.handler.truncate()
        self.handler.write(text)

    def close(self):
        self.handler.close()


But if there is Cyrillic in JSON, then when you try to open it, there will be an error, and Cyrillic is saved in Unicode.
How to fix this and correctly save the Cyrillic alphabet using the methods of this class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deleting Account, 2020-05-13
@Enot_Star

https://ru.stackoverflow.com/questions/584129/%D0%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question