Py3: decode subprocess output

Gnarlodious gnarlodious at gmail.com
Wed Nov 3 00:16:11 EDT 2010


Under Python 2.6, commands.getoutput returns text type str containing
ANSI Terminal formatting hex characters:

"\x1b[1;31mSun : \x1b[1;36m114.902\x1b[0m   - 0\xf800' (-)\x1b[1;33m

I have a system for parsing out the relevant parts and I prefer to
keep using that system.

Under Python 3, subprocess.check_output returns a bytestring that
doesn't parse. Since the CLI program (written in the 1990's) will
never send Unicode, is there a way to downconvert the bytestring into
type str so as to emulate Py2.6 behavior?

Or alternatively, is there a whizbang Py3 method to parse out strings
delimited by hex? I tried several tricks but it is all beyond my
skills.

Thanks.

-- Gnarlie



More information about the Python-list mailing list