E
E
Evgeny Trofimov2017-05-07 21:22:31
Python
Evgeny Trofimov, 2017-05-07 21:22:31

How to save unicode as cyrillic (python 2.7)?

There is a json line like

{u'name': u'\u0411\u043e\u0435\u0432\u043e\u0439 \u043a\u043b\u0438\u0447'}

I need to save it to a file, but in plain Cyrillic.
I do it like this:
data=json.dumps(json_data)
filename = 'test.txt'
out = open(filename, 'wb')
out.write(data.encode("utf-8"))
out.close()

But it still stays like this
u'\u0411\u043e\u0435\u0432\u043e\u0439 \u043a\u043b\u0438\u0447
instead of Cyrillic.
But if you just output this line through print (), then it is displayed in Russian.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-05-07
@deadrime

https://docs.python.org/2/library/json.html#charac...

import json
json_data = {'ф': "і"}
filename = '/tmp/test.txt'
with open(filename, 'w') as out:
   out.write( json.dumps(json_data, ensure_ascii=False))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question