P
P
puker-ti2015-01-01 20:22:32
Python
puker-ti, 2015-01-01 20:22:32

How to correctly print unicode characters to a file in python?

The essence of the question is this: I print a json structure containing Russian letters in a file, when I open the file, instead of Russian letters, I see unicode encoded characters (\u...). Tell me how to write to a file, so that then exactly the necessary characters are displayed and not their codes, or an editor that can translate this business (for linux (except np++ on vine)).
I print like this:

with open(path, 'w+') as f:
            json.dump(data, f, indent=2)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2015-01-01
@puker-ti

with open(path, 'w+') as f:
      f.write(json.dumps(data, indent=2, ensure_ascii=False).encode('utf-8'))

G
GavriKos, 2015-01-01
@GavriKos

Try like this:

def SaveJson(filename, value):
    outfile = codecs.open(filename, 'w', "utf-8")
    str = json.dumps(value, ensure_ascii=False, indent=4)
    outfile.write(str)
    outfile.close()

It works in the second python.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question