English 中文(简体)
How to print out encoded Asian characters(gb2312) on command prompt?
原标题:

I am working for a company that uses the Python programming language version 3.1 as a causal work now. And I ve encountered this problem: how to print out some encoded Asian characters(Chinese, Japanese, Korean) on command prompt?

Done a bit research and tried, but got no luck:

import sys
import codecs
print(sys.getdefaultencoding()) # prints out UTF-8
fileObj = codecs.open("test.txt", "r", "eucgb2312_cn")
content = fileObj.read()
print(content)

It is the last line that would cause this error:

C:Documents and SettingsMichael MaoDesktop>test.py
utf-8
Traceback (most recent call last):
  File "C:Documents and SettingsMichael MaoDesktop	est.py", line 6, in <module>
    print(u)
  File "C:	oolsPython31libencodingscp437.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 >

I cannot change the default encoding from UTF-8 to anything else, so I reckon that is the problem preventing the output from being rendered correctly.

Can anyone help me out in this? Thanks a lot in advance!

最佳回答

I have solved this problem. When I am programming a dict, I encounter this problem.

#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()
问题回答

I cannot change the default encoding from UTF-8 to anything else

I don t think UTF-8 is being used as the default encoding for your console:

File "C: oolsPython31libencodingscp437.py"

cp437 is the old DOS terminal code page, which indeed cannot print chinese characters.

See bug 1602 for a batch file hack to make Windows and Python 3 use UTF-8 (code page 65001) for the console, but in general the console has always been pretty broken for non-ASCII characters, and will continue to be so until someone changes Python to use WriteConsoleW instead of the standard C IO functions.

If you open the cmd window yourself, type the following command before running test.py: mode con cp select=936

If your Python program starts by some other means, you ll have to make it open its console window with the correct code page.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签