Search code examples
pythoncmdpython-3.7mks-integrityptc-windchill

Why is Python's subprocess output different than cmd's of a PTC command?


Im using Python 3.7 and the im viewissue <itemID> command of Integrity Client 12.5. When I query and item using this in cmd, I get every detail perfectly decoded, no unknown bytes or hexadecimal values. However, when I try using

subprocess.run(<command_args>, capture_output=True).stdout.decode(encoding='utf-8')

I get this exception:

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 528: invalid start byte"

And the problem is there with every other encoding type I tried. But when I use the exact same command in cmd I don't see any problematic character, everything is displayed correctly and if I checked right it uses UTF-8 as well.

There are some encodings that can display that character (e.g. Windows-1252), however the decoded character is different than it should be. Byte 0xb5 translates to "µ" but originally that should be an "Á", as it is in the cmd output.

I tried using different encodings, different functions but couldn't manage to find what causes this difference between the two platforms' output or how I can overcome the problem aside from manually mapping the characters to the correct ones.


Solution

  • Thank you all in the comments who helped.

    The solution was finding which code page cmd was using with chcp or mode, then using that as the specified encoding in the Python script like

    stdout.decode(encoding='cp852')
    

    It was cp852.