Answer the question
In order to leave comments, you need to log in
How to output russian characters in python file?
There is a lightweight Python 2 program that goes through one file with Russian words and adds them to a collection, then this collection is written to a json file
f = open("in.txt")
conversations = open("data/russian/conversations.json", "wb")
ar = {"conversations": []}
worker = []
for line in f:
if line == "-----":
ar["conversations"].append(worker)
worker = []
else:
worker.append(line.strip())
print ar["conversations"][0][0]
conversations.write(str(ar))
conversations.close()
{'conversations': [['\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!', '\xd0\x97\xd0\xb4\xd1\x80\xd0 \xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9!', // and so on
Answer the question
In order to leave comments, you need to log in
import codecs
import json
with codecs.open('file.txt', 'w', encoding='utf-8') as fout:
json.dump({u'абв': u'где'}, fout, ensure_ascii=False)
[[email protected] py]$ cat file.txt
{"абв": "где"}[[email protected] py]$
Try something like this:
# -*- codecs: utf-8 -*-
import codecs
file = codecs.open("somefile", "w", "utf-8")
file.write(u'какая-то строка')
file.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question