Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在文件中写入俄文字符?_Python_Windows_Unicode_Python 2.x_Python Unicode - Fatal编程技术网

Python 如何在文件中写入俄文字符?

Python 如何在文件中写入俄文字符?,python,windows,unicode,python-2.x,python-unicode,Python,Windows,Unicode,Python 2.x,Python Unicode,在控制台中,当我尝试输出俄语字符时,它会给我 谁知道为什么 我试着写文件——在这种情况下也是这样 比如说 f=open('tets.txt','w') f.write('some russian text') f.close 内部文件是-/ 或 另外,记事本不允许我用俄语字母保存文件。我谨此陈辞: 此文件包含中的字符 Unicode格式,如果 将此文件保存为ANSI编码的文件 文本文件。要保留Unicode 有关详细信息,请单击下面的“取消”,然后单击 然后选择一个Unicode选项 从“编码

在控制台中,当我尝试输出俄语字符时,它会给我

谁知道为什么

我试着写文件——在这种情况下也是这样

比如说

f=open('tets.txt','w')
f.write('some russian text')
f.close
内部文件是-/

另外,记事本不允许我用俄语字母保存文件。我谨此陈辞:

此文件包含中的字符 Unicode格式,如果 将此文件保存为ANSI编码的文件 文本文件。要保留Unicode 有关详细信息,请单击下面的“取消”,然后单击 然后选择一个Unicode选项 从“编码”下拉列表中。 继续


如何调整我的系统,这样我就不会有这个问题。

您使用的是什么控制台?很可能,您的主机不支持该语言。确保您的控制台支持Unicode,并且您的应用程序正在发送Unicode字符串

更新:


要解决有关Windows记事本问题的更新:单击文件->另存为,然后从编码下拉列表中选择Unicode。

如果文件编码包含非ASCII字符,则需要定义文件编码


您是否也在控制台中键入,还是只在控制台中键入结果?正如petraszd所说,这看起来是pep-0263的问题

print p.decode('your-system-encoding')
应该在控制台工作我不知道你用的俄语编码系统是什么


如果您使用的是.py文件,则需要在文件顶部放置-*-编码:UTF-8-*-将UTF-8替换为俄罗斯编码,我认为如果您的操作系统配置了正确的编码,则不需要打印.decode。至少我不需要它,但我不知道它如何与俄语兼容

尝试使用编解码器打开文件,您需要

import codecs
然后

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

以下是一个已制定的示例,请阅读评论:

#!/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

这个问题的题目选得太差了!真的吗?或者更确切地说�?@秋波:那是什么?当目标无法存储给定字符(因为它超出字符集范围)时使用。例如,数据库和输出文件/stdout/etc写入程序。这个� 当目标能够显示给定的字符,但由于该字符超出其指示使用的字符集的范围而不显示时,使用。例如,网络浏览器。总而言之,这有道理吗?“是在这里使用的。@Carl-我想说的是,海报只是让他们在沉思和神秘的暗流中变得悲剧性。@Philipp:我完全同意这一点,但不幸的是,在许多地方,真相在许多语言中是不同的。这些未知字符将被丢弃或替换。目标并不清楚。你试过哪些控制台?你在用什么操作系统?您能否使用Python以外的编程语言成功地将俄语字符输出到您的控制台?我没有注意到它是何时发生的。但在我安装了像PYMSSQL和ODBC这样的扩展之后,它发生了。如果在安装扩展后行为发生了变化,请取消安装扩展并查看旧行为是否恢复。扩展引入意外问题并非闻所未闻。@user375373:如果您回答了bta的问题,将非常有帮助。这没有帮助!甚至当我在记事本中保存俄语文本时,它也会告诉我无法保存。因为我丢失了数据。请按照记事本给出的建议,选择一种Unicode编码。请不要在记事本中编码。这真是个非常糟糕的主意。尝试vim或emacs。如果这两个太吓人了-试试记事本++或Scite或比记事本更理智的东西。@petraszd:我们这里只使用记事本,因为它支持Unicode。当我打字时,一切都好。当我把这个--coding:UTF-8--放到文件中时,我得到了错误:SyntaxError:non-ASCI character'\xff',我得到了错误:SyntaxError:non-ASCI character'\xff',在文件'my python file'中,但是没有编码声明我声明了编码,这就是第二行的目的。并且文件中没有字符“\xff”。你确定你做的每件事都正确吗?所有的字符都正确地显示在记事本中吗?
#!/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