如何打印出编码亚洲字符(GB2312)的命令提示符?
-
21-09-2019 - |
题
我工作的一个使用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 >
我不能改变从UTF-8编码为任何其他默认的,所以本人认为是防止被正确呈现的输出的问题。
谁能帮助我在这?非常感谢事先!
解决方案
我已经解决了这个问题。当我编程一个字典,我遇到这个问题。
#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()
其他提示
我不能从UTF-8更改默认编码为任何其他
我不认为UTF-8被用作您的控制台上默认的编码:
文件 “C:\工具\ Python31 \ lib中\编码\ cp437.py”
CP437是旧DOS终端代码页,这的确不能打印中国字符。
请参阅错误1602的批处理文件劈,使视窗和Python 3使用UTF-8(代码页65001)为控制台,但在一般的控制台一直相当打破了非ASCII字符,并且将继续这样做,直到有人改变Python中使用,而不是标准的C IO函数WriteConsoleW。
如果你自己打开cmd窗口,运行test.py之前键入以下命令: 模式CP CON选择= 936
如果通过其他方式你的Python程序将启动,你就必须让它打开其控制台窗口,正确的代码页。
不隶属于 StackOverflow