How to write Russian characters in file?

In console when I'm trying output Russian characters It gives me ???????????????

Who know why?

I tried write to file - in this case the same situation.

for example

f.write('some russian text')

inside file is - ?????????????????????????/


p="some russian text"
print p

In additional Notepad don't allow me to save file with Russian letters. I give this:

This file contains characters in Unicode format which will be lost if you save this file as an ANSI encoded text file. To keep the Unicode information, click Cancel below and then select one of the Unicode options from the Encoding drop down list. Continue?

How to adjust my system, so I will don't have this problems.


Here is a worked-out example, please read the comments:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# The above encoding declaration is required and the file must be saved as UTF-8

from __future__ import with_statement   # Not required in Python 2.6 any more

import codecs

p = u"абвгдежзийкл"  # note the 'u' prefix

print p   # probably won't work on Windows due to a complex issue

with"tets.txt", "w", "utf-16") as stream:   # or utf-8
    stream.write(p + u"\n")

# Now you should have a file called "tets.txt" that can be opened with Notepad or any other editor

Try opening the file using codecs, you need to

import codecs

and then

writefile ='write.txt', 'w', 'utf-8')

You need to define file encoding if it contains non-ASCII chars.

Try p=u"some russian text"

What console are you using? Chances are, your console doesn't support that language. Make sure that your console supports Unicode (and that your app is sending Unicode strings).


To address the update to your question regarding problems with Windows' Notepad: Click File->Save As, and then choose "Unicode" from the "Encoding" drop-down list.

Are you typing in console too or only seing the results in console? This looks a pep-0263 problem as petraszd said.

print p.decode('your-system-encoding')

should work in console (I don't know what is the encoding system you use for Russian)

If you are using a .py file, you need to place # -*- coding: UTF-8 -*- (replacing utf-8 with Rusian encoding) on the top of the file and I think there is no need for the .decode in print if your OS is configured with the right encoding. (at least I don't need it but I don't know how it works with Russian)


 ? Django and MySql - Encoding difficulties
 ? django model forms encoding
 ? decode/encode error django python
 ? Django uses ANSI_X3.4-1968 encoding instead of UTF-8
 ? Cannot open filename that has umlaute in python 2.7 & django 1.9
 ? How to save a list of directory with files containing Russian or Chinese characters (Windows7)
 ? How to save a list of directory with files containing Russian or Chinese characters (Windows7)
 ? How to save a list of directory with files containing Russian or Chinese characters (Windows7)
 ? What encoding/code page is cmd.exe using?
 ? Recursive directory listing using Ruby with Chinese characters in file names