Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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 如何在命令提示符下打印编码的亚洲字符(gb2312)?_Python_Windows_Python 3.x_Command Prompt - Fatal编程技术网

Python 如何在命令提示符下打印编码的亚洲字符(gb2312)?

Python 如何在命令提示符下打印编码的亚洲字符(gb2312)?,python,windows,python-3.x,command-prompt,Python,Windows,Python 3.x,Command Prompt,我现在为一家使用Python编程语言3.1版的公司工作。 我遇到了这个问题:如何在命令提示符下打印出一些编码的亚洲字符(中文、日文、韩文) 做了一些研究并尝试了一下,但没有成功: import sys import codecs print(sys.getdefaultencoding()) # prints out UTF-8 fileObj = codecs.open("test.txt", "r", "eucgb2312_cn") content = fileObj.read() prin

我现在为一家使用Python编程语言3.1版的公司工作。 我遇到了这个问题:如何在命令提示符下打印出一些编码的亚洲字符(中文、日文、韩文)

做了一些研究并尝试了一下,但没有成功:

import sys
import codecs
print(sys.getdefaultencoding()) # prints out UTF-8
fileObj = codecs.open("test.txt", "r", "eucgb2312_cn")
content = fileObj.read()
print(content)
这是导致此错误的最后一行:

C:\Documents and Settings\Michael Mao\Desktop>test.py
utf-8
Traceback (most recent call last):
  File "C:\Documents and Settings\Michael Mao\Desktop\test.py", line 6, in <module>
    print(u)
  File "C:\tools\Python31\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u5377' in position 3: character maps to < undefined >
C:\Documents and Settings\Michael Mao\Desktop>test.py
utf-8
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\Michael Mao\Desktop\test.py”,第6行,在
打印(u)
文件“C:\tools\Python31\lib\encodings\cp437.py”,第19行,在encode中
返回codecs.charmap\u encode(输入、自身错误、编码\u映射)[0]
UnicodeEncodeError:“charmap”编解码器无法对位置3中的字符“\u5377”进行编码:字符映射到
我无法将默认编码从UTF-8更改为任何其他编码,因此我认为这是导致输出无法正确呈现的问题


有人能帮我吗?提前多谢

如果您自己打开cmd窗口,请在运行test.py之前键入以下命令: 模式con cp select=936

如果您的Python程序以其他方式启动,则必须使用正确的代码页使其打开控制台窗口

我无法将默认编码从UTF-8更改为任何其他内容

我不认为UTF-8被用作控制台的默认编码:

文件“C:\tools\Python31\lib\encodings\cp437.py”

cp437是旧的DOS终端代码页,它确实不能打印汉字


有关使Windows和Python3在控制台上使用UTF-8(代码页65001)的批处理文件攻击,请参阅,但一般来说,控制台对于非ASCII字符一直非常糟糕,并且将继续如此,直到有人将Python更改为使用WriteConsoleW而不是标准的C IO函数。

我已经解决了这个问题。当我编写dict时,我遇到了这个问题

#coding=utf-8
import codecs
import sys
# import imp
# imp.reload(sys) 
# sys.setdefaultencoding('utf8')
dictFileName = 'abstract.dict'
print(sys.getdefaultencoding())  
print(sys.stdout.encoding)

def readDict():
    print("start reading dict...")
    #dictObject = codecs.open(dictFileName,'rb', encoding = 'utf-8')#, encoding = 'utf-8')
    dictObject = open(dictFileName, 'rb')
    try:
        print('open file success!')
        #dictObject.seek(0x1852c)
        chunk = dictObject.read(0x5f0) #0x5f0
        print(len(chunk))
        #chunk = dictObject.read(0x1)
        print('read success')
        #print(chunk.decode("utf-8"))
        #print(chunk.encode('utf-8').decode('gb18030'))
        #sys.stdout.buffer.write(chunk.encode('gb18030'))
        sys.stdout.buffer.write(chunk.decode('utf-8').encode('gb18030'))
    finally:
        dictObject.close()
readDict()
input()

我可以从你的test.txt中得到一些文本吗?好吧,我只是从用中文写的在线新闻中抓取一些工作,并用GB2312编码保存到磁盘上……哦,正如S.Mark所建议的,我试着使用print(content.encode(“u8”)和print(content.encode('eucgb2312_cn'))但两人都给了我一些奇怪的输出:xd5\xc2\xbd\xda\xb8\xfc\xb6\xe0\xa3\xd6\xd5\xfd\xb3\xb0\xe6\xd4\xc4\xb6\xc1\xa3\xa1)\r\n我认为关键是:sys.stdout.buffer.write(chunk.encode('gb18030'))ps:codes.open(dictFileName,'r',encoding='utf-8'),encoding='utf-8'是必不可少的。