本文介绍了python子流程编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将powershell的输出存储在var中:

I'm trying to store the output of powershell in a var:

import subprocess
subprocess.check_call("powershell \"Get-ChildItem -LiteralPath 'HKLM:SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall' -ErrorAction 'Stop' -ErrorVariable '+ErrorUninstallKeyPath'\"", shell=True, stderr=subprocess.STDOUT)

这样,使用 check_call ,它可以打印ok,例如:

this way, using check_call, it prints ok, for example:

DisplayName: Skype™

DisplayName : Skype™

但是这种方式只能打印到屏幕上,所以我必须使用

but this way it only prints to screen, so i have to use

import subprocess
output = subprocess.check_output("powershell \"Get-ChildItem -LiteralPath 'HKLM:SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall' -ErrorAction 'Stop' -ErrorVariable '+ErrorUninstallKeyPath'\"", shell=True, stderr=subprocess.STDOUT)

,然后使用 check_output ,我得到:

DisplayName:SkypeT

DisplayName : SkypeT

我该如何解决?

推荐答案

按照此发布说明进行操作:

Following this post instructions:How to make Unicode charset in cmd.exe by default?

是否可以绕过此编码问题

Its possible to bypass this encoding problem

import subprocess
output = subprocess.check_output("chcp 65001 | powershell \"Get-ChildItem -LiteralPath 'HKLM:SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall' -ErrorAction 'Stop' -ErrorVariable '+ErrorUninstallKeyPath'\"", shell=True, stderr=subprocess.STDOUT)

这篇关于python子流程编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:40