skip to Main Content

This stdout doesn’t work in UTF-8 as my Visual Studio Code and my computer (chcp 850)

 Estado de los medios. . . . . . . . . . . : medios desconectados
   Sufijo DNS espec¡fico para la conexi¢n. . :
   Descripci¢n . . . . . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
   Direcci¢n f¡sica. . . . . . . . . . . . . : E0-C2-64-64-FA-92
   DHCP habilitado . . . . . . . . . . . . . : s¡
   Configuraci¢n autom tica habilitada . . . : s¡

Code:

import subprocess

def ejecutar_comando(comando_Red):
    resultado = subprocess.run(comando_Red, shell=True, capture_output=True, text=True)
    return resultado.stdout.strip('UTF-8')

comando_Red = [
    "ipconfig /all",
    "getmac"
]

for comando_Red in comando_Red :
    salida = ejecutar_comando(comando_Red)
    print (salida)

I was expecting an UTF-8 output with things like í, á, ó, ú….
my cmd and powershell work in chcp 850 and show proper Latin characters.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks everyone, i solved by adding cp850 and changing a little bit the structure. Asuming i just had kind of a mess at printing the output, then my cmd just went crazy.

    import subprocess

    Execute the command and capture output

    result = subprocess.run(['systeminfo'], capture_output=True, text=True, encoding='cp850')

    print output

    print(result.stdout)


  2. Your understanding of character encoding is clearly incomplete. If the console is configured for code page 850 then that’s what you should tell Python, and expect from the output (and not, for example, UTF-8).

    Assuming that the subprocess produces output in this encoding,

    def ejecutar_comando(comando_Red):
        resultado = subprocess.run(
            comando_Red, capture_output=True, text=True, encoding='cp850')
        return resultado.stdout
    

    The crucial addition is encoding='cp850' which tells Python how to decode the output from the subprocess.

    .strip('UTF-8') would remove any occurrence of U, T, F, -, or 8 on either side of the string, but that’s clearly not required or correct here.

    >>> "fnord".strip("fd")
    'nor'
    

    Tangentially, I don’t believe shell=True is necessary or useful here, so I removed that, too.

    This still requires your Python to be configured to correctly encode output for the current code page. Please refer to Python, Unicode, and the Windows console for details about that.

    More likely, your console is actually cp1252.

    >>> "configuración".encode("cp850").decode("cp1252")
    'configuraci¢n'
    

    If you need help identifying encodings, I humbly offer you https://tripleee.github.io/8bit/

    >>> "conciguración".encode("cp1252")
    b'conciguracixa2n'
    

    If you have xa2 and want to check in which encodings that corresponds to ó, see https://tripleee.github.io/8bit/#a2

    Perhaps see also the Stack Overflow character-encoding tag info page

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search