I have xmpp bot written in python. One of it’s plugins is able to execute OS commands and send output to the user. As far as I know output should be unicode-like to send it over xmpp protocol. So I tried to handle it this way:
output = os.popen(cmd).read()
if not isinstance(output, unicode):
output = unicode(output,'utf-8','ignore')
bot.send(xmpp.Message(mess.getFrom(),output))
But when Russian symbols appear in output they aren’t converted well.
sys.getdefaultencoding()
says that default command prompt encoding is ‘ascii’, but when I try to do
output.decode('ascii')
in python console I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 1:
ordinal not in range(128)
OS: Win XP, Python 2.5.4
PS: Sorry for my English 🙁
You say “””sys.getdefaultencoding() says that default command prompt encoding is ‘ascii'”””
sys.getdefaultencoding says NOTHING about the “command prompt” encoding.
On Windows,
sys.stdout.encodingshould do the job. On my machine, it containscp850when Python is run in a Command Prompt window, andcp1252in IDLE. Yours should containcp866andcp1251respectively.Update You say that you still need cp866 in IDLE. Note this:
So when your app starts up, check if you are on Windows and if so, parse the result of
os.popen('chcp').read(). The text before the:is probably locale-dependent.codepage = result.split()[-1]may be good enough “parsing”. On Unix, which doesn’t have a Windows/MS-DOS split personality,sys.stdout.encodingshould be OK.