如何在文件中写俄文字符?
作者:互联网
在我正在尝试输出俄语字符的控制台它给了我??????????????
谁知道为什么?
我尝试写入文件 – 在这种情况下相同的情况.
例如
f=open('tets.txt','w')
f.write('some russian text')
f.close
里面的文件是 – ??????????????????????
要么
p="some russian text"
print p
?????????????
在额外的记事本中,不允许我用俄文字母保存文件.我这样说:
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?
如何调整我的系统,所以我不会有这个问题.
解决方法:
这是一个经过深思熟虑的例子,请阅读评论:
#!/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 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
标签:python-unicode,python,unicode,windows,python-2-x 来源: https://codeday.me/bug/20191001/1840669.html