pythonvisual-studio-codecmdutf-8output

Already have UTF-8 in vsc (python) but stdout makes strange characters


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.


Solution

  • 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)