How to write Russian characters in file?
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-8from __future__ import with_statement # Not required in Python 2.6 any moreimport codecsp = u"абвгдежзийкл" # note the 'u' prefixprint p # probably won't work on Windows due to a complex issuewith codecs.open("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 = codecs.open('write.txt', 'w', 'utf-8')
You need to define file encoding if it contains non-ASCII chars.