Py3: decode subprocess output

Discussion in 'Python' started by Gnarlodious, Nov 3, 2010.

  1. Gnarlodious

    Gnarlodious Guest

    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
     
    Gnarlodious, Nov 3, 2010
    #1
    1. Advertising

  2. > 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?


    What do you mean by "that doesn't parse"? Returning a byte string is
    *already* emulating 2.6's behaviour: in 2.6, you also get a byte string,
    and most likely the very same string.

    > 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.


    Please understand that there really aren't "hex characters" here. Each
    individual character is a byte, i.e. "Sun" == "\x53\x75\x6d". So the
    strings you get aren't delimited by a "hex character", but by a "control
    character".

    That said: how did you try to parse it, and how did that fail?

    Regards,
    Martin
     
    Martin v. Loewis, Nov 3, 2010
    #2
    1. Advertising

  3. Gnarlodious

    Tim Harig Guest

    On 2010-11-03, Gnarlodious <> wrote:
    > 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?


    str() will convert a bytestring into a Python string object which you will
    be able to parse so long as the output remains ASCII (which your example
    is). If the input uses another encoding or contains characters with the
    high order bit set, you might need to fiddle with the strings encoding.
     
    Tim Harig, Nov 3, 2010
    #3
  4. Gnarlodious

    Gnarlodious Guest

    OK, it turns out I had to tweak the parsing minimally for Python3:

    1) Substrings such as st[5] no longer work (returns an ascii number),
    instead st[5:6] selects one character

    2) Replacements need to specified as bytes: s.replace('R','*') change
    to s.replace(b'R',b'*')

    So I think this problem is solved.

    -- Gnarlie
     
    Gnarlodious, Nov 3, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Collin Winter

    Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    375
    Collin Winter
    Sep 23, 2005
  2. Collin Winter

    Re: Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    348
    Collin Winter
    Sep 24, 2005
  3. John Machin
    Replies:
    0
    Views:
    433
    John Machin
    Jul 16, 2008
  4. Gnarlodious

    Py3: Terminal or browser output?

    Gnarlodious, Feb 13, 2010, in forum: Python
    Replies:
    5
    Views:
    316
    John Bokma
    Feb 13, 2010
  5. hiral
    Replies:
    2
    Views:
    629
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page