Convert from ascii to utf-8 with Python

I have an xmpp bot written in python. One of its plugins is able to execute OS commands and send output to the user. As far as I know, the output should be unicode-like for sending via xmpp protocol. So I tried to handle it like this:

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 characters appear on the output, they are poorly converted.

sys.getdefaultencoding() 

says the default command line 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: (

+3
source share
4 answers

, "" sys.getdefaultencoding() , - 'ascii' "" "

sys.getdefaultencoding .

Windows, sys.stdout.encoding . cp850, Python cp1252 IDLE. cp866 cp1251 .

. , cp866 IDLE. :

IDLE 2.6.4      
>>> import os
>>> os.popen('chcp').read()
'Active code page: 850\n'
>>>

, , , Windows, , os.popen('chcp').read(). :, , . codepage = result.split()[-1] "". Unix, Windows/MS-DOS, sys.stdout.encoding .

+1

sys.getdefaultencoding() python - ASCII, . ASCII .

, , , .

- :

import locale
encoding = locale.getpreferredencoding(do_setlocale=True)¶
+3

Ascii 127 0x7F. , ? 866

. http://en.wikipedia.org/wiki/Code_page

edit: , 886 , , , 886 - . , , , .

+2

Python 'cp855', 'cp866', 'cp1251', 'iso8859_5', 'koi8_r' - . , popen. Windows "chcp" , . , Windows. Windows Windows "cp437", "cp1252" , "".

0

Source: https://habr.com/ru/post/1732778/


All Articles