- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法让 subprocess.getoutput 处理不是有效 UTF-8 的输出?如果命令的输出返回非 UTF-8 字符,则会引发异常。但是,该函数似乎没有编码参数。
File "foo.py", line 104, in <module>
OutText = subprocess.getoutput(TheCmd)
File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 827, in getoutput
return getstatusoutput(cmd)[1]
. . .
File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 1495: invalid continuation byte
最佳答案
您可以尝试另一个带有选项 test=True
的命令 subprocess.check_output
。这个函数的结果是字节。然后您可以使用正确的编解码器手动解码字节以获得字符串结果。像这样:
import subprocess
cmd = "svn log"
result_bytes = subprocess.check_output(cmd, text=False)
result = result_bytes.decode("gb2312")
print(result)
关于具有非 UTF-8 输出的 Python 的 subprocess.getoutput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113630/
这个问题在这里已经有了答案: Store output of subprocess.Popen call in a string [duplicate] (15 个回答) 关闭4年前。 我想从 pyt
我正在努力用子进程替换 commands.getoutput。请看下面的代码: import subprocess import commands dir = subprocess.check_out
我有一个 java 程序,我必须从 python 运行,获取输出并将结果存储在 3 个变量中(如果它按预期工作,应该打印 3 个值,每个值在不同的行上)。 这是我正在使用的代码: stdout1 =
我在 Windows 中使用 python 3.6,我的目标是运行 cmd 命令并将输出作为字符串保存在变量中。我正在使用子进程及其对象,例如 check_output、Popen and Commu
问题看起来很简单,但是经过一天的谷歌搜索和查看 stackoverflow 后我找不到任何解决方案。最初我正在开发一个简单的等 ionic 体,它将每 30 分钟向本地网络服务器发送一个特定请求,解析
python : print("Active Device:"+subprocess.getoutput("ip link|grep \"state UP\"|cut -d : -f 2|cut -d
我正在使用 Ubuntu 12.4,并且安装了 matlab。通常,为了从终端调用 matlab,我必须输入“~/MATLAB/bin/matlab”。显然这有点烦人,所以我通过添加 为这个命令起别名
commands.getoutput() 在当前的 Python 2.x 中被弃用,并在 Py3k 中被移除。 如何在干净的 Python 3 中获取类似信息? 最佳答案 逐字等效为 subproce
本文整理了Java中org.gradle.api.tasks.bundling.Zip.getOutputs()方法的一些代码示例,展示了Zip.getOutputs()的具体用法。这些代码示例主要来
有没有办法让 subprocess.getoutput 处理不是有效 UTF-8 的输出?如果命令的输出返回非 UTF-8 字符,则会引发异常。但是,该函数似乎没有编码参数。 File "foo.py
自从在 Android Gradle 插件 0.13.0 中升级到 gradle 2.1 后,这个问题就出现了,但我一直无法理解为什么有时会记录此警告。 考虑此 block 以根据变体类型重命名 AP
本文整理了Java中org.embl.ebi.escience.scufl.enactor.WorkflowInstance.getOutput()方法的一些代码示例,展示了WorkflowInsta
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType.getOutput()方法的一些代码示例,展示了
无法找到方法'com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()Ljava/util/List 无法找到方法'
错误:无法找到方法“com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;”。此意外错误的可能原因包括: Gradl
构建.gradle buildscript { ext.kotlin_version = '1.1.51' repositories { jcenter()
这是我和 friend 在使用Android Studio 2.3.1时开发的一个项目。升级到 Android Studio 3.0 后。发生此错误: Error:Unable to find met
Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
尝试使用 Android Studio 3.0 Canary 1 启动新的 Kotlin 项目会显示此错误。完整跟踪: Error:Unable to find method 'com.android
我是一名优秀的程序员,十分优秀!