解析命令调用的输出

2023-11-30

因此,我尝试从 python 执行 shell 命令,然后将其存储在数组中或直接解析管道 shell 命令。

我通过 subprocess 命令管道传输 shell 数据,并使用 print 语句验证输出,它工作得很好。

a = subprocess.Popen('filepath/command', shell=True, stdout=subprocess.PIPE)
b = a.stdout.read()
print(b)

现在,我正在尝试从未知数量的行和 6 列中解析出数据。由于 b 应该是一个长字符串,因此我尝试解析该字符串并将显着字符存储到另一个要使用的数组中,但我想分析数据。

i = 0
a = subprocess.Popen('filepath/command', shell=True, stdout=subprocess.PIPE)
b = a.stdout.read()
for line in b.split("\n\n"): #to scan each row with a blank line separating each row
    salient_Chars[i, 0] = line.split(" ")[3] #stores the third set of characters and stops at the next blank space
    salient_Chars2[i, 0] = line.split(" ")[4] #stores the fourth set of characters and stops at the next blank space
    i = i + 1

我收到错误 [TypeError:需要类似字节的对象,而不是“str”]。我搜索了这个错误,这意味着我使用 Popen 存储了字节而不是字符串,我不确定为什么,因为我使用 print 命令验证了它是一个字符串。在搜索如何将 shell 命令通过管道传输到字符串中后,我尝试使用 check_output 。

from subprocess import check_output
a = check_output('file/path/command')

这给了我一个权限错误,所以如果可能的话我想使用 Popen 命令。

如何将管道 shell 命令转换为字符串,然后如何正确解析分为行和列的字符串,列之间有空格,行之间有空行?


Quoting 亚伦·曼帕's answer:

您需要解码字节对象以生成字符串:

>>> b"abcde"
b'abcde'

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8") 
'abcde'

因此你的代码将如下所示:

i = 0
a = subprocess.Popen('filepath/command', shell=True, stdout=subprocess.PIPE)
b = a.stdout.read().decode("utf-8") # note the decode method
for line in b.split("\n\n"): #to scan each row with a blank line separating each row
    salient_Chars[i, 0] = line.split(" ")[3] #stores the third set of characters and stops at the next blank space
    salient_Chars2[i, 0] = line.split(" ")[4] #stores the fourth set of characters and stops at the next blank space
    i = i + 1

顺便说一句,我不太明白你的解析代码,这会给你一个TypeError: list indices must be integers, not tuple因为您将一个元组传递给列表索引salient_Chars(假设它是一个列表)。

Edit

请注意,调用print内置方法是not一种检查传递的参数是否是普通字符串类型对象的方法。来自引用答案的OP:

communicate() 方法返回一个字节数组:

>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'

但是,我想将输出作为普通的 Python 字符串来处理。 这样我就可以像这样打印它:

>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解析命令调用的输出 的相关文章

随机推荐

  • Matlab神经网络,如何强制使用某些集合进行训练、验证和测试?

    如果您使用 GUI nnstart 则仅提供输入和输出 但您无法决定哪些行将作为训练 验证和测试集 因为它们是随机选择的 如何手动指定它们 您可以使用从 NNSTART 启动的任何 GUI 来创建一些示例训练代码 然后通过设置以下数据划分值
  • 从 Base64 编码字符串中检索 ECC 公钥

    我一直在尝试创建一个实例java security PublicKey使用 Base64 编码的 ECC 公钥 MainActivity java Override protected void onCreate Bundle savedI
  • 生成的 SVG 图像不显示

    我正在开发一个 JavaScript 类来显示所有 SVG 对象 但是当我创建元素 image 时 浏览器不会显示它 但是 如果我复制生成的代码并将其放入另一个文档中 则会显示图像 当我使用 Firebug 的检查器搜索图像时 出现了对象但
  • EJB 3.1:是否允许将 Bean 注入到不受容器管理的资源中?

    我使用的是 JBoss 6 1 它不完全符合 EJB 3 1 截至目前 我无法通过以下方式将 EJB 注入到我的 Struts 操作类 或任何非 Java EE 容器管理的类中 EJB 但是当 EJB 3 1 规范完全实现时这可能吗 如果没
  • Spring Security 注销后退按钮

    spring security有办法防止下面最后一点吗 我用的是3 0 5 用户登录我的网站 用户转到网站中的任何页面并单击注销 注销链接使用户会话无效并将其发送到我网站的登录页面 在同一浏览器中 用户导航到新网站 例如 cnn com 用
  • 无法在android中的graph api中获取性别

    我无法在这里获取性别OnCompleted功能 我可以获得其他参数 例如id name email 目前应用程序处于开发模式 这是昨天工作的 Code GraphRequest request GraphRequest newMeReque
  • 使用“ADODB.Stream”将ANSI转换为UTF-8,第一行丢失1-2个字符

    我需要将 ANSI csv 文件转换为 UTF 8 csv 文件 下面的代码可以工作 但第一个字符丢失 请参阅随附的屏幕截图 原始文件 客户 输出文件 客户 Function Convert myFileIn myFileOut Dim s
  • JavaScript this 来自 jQuery this

    有没有办法从 jQuery this 获取 JavaScript this this this 任何this is this不是 jquery 而是一个特殊的 有些复杂的 javascript 关键字 用于描述当前的执行范围 您的挑战可能是
  • 无法识别 Visual Studio 代码任务构建“C:\Program”

    因此 我使用 Visual Studio Code 来构建并运行一个简单的 C 程序 我使用tasks json来处理编译 基于此 如何使用 VS Code 和 cl 编译 C 代码 See https go microsoft com f
  • Pymssql 是否可以与 MS SQL Server 建立安全连接 (SSL)?

    我正在使用 Python 代码 Pymssql 库 从 MS SQL 服务器进行查询 但是我想知道是否有任何方法可以使连接安全并加密从服务器发送到 python 的数据 Thanks 是的 它可以 您需要通过 OpenSSL 支持 SSL
  • boost:thread - 编译器错误

    我想在程序中使用 boost thread 但出现以下编译器错误 Visual Studio 2005 Error 1 error C2064 term does not evaluate to a function taking 0 ar
  • 将一个方阵中的 NA 添加并将其放入另一个方阵中

    我想知道是否可以选择NA列和NA行从d1方阵并将它们放入d2方阵来实现我的DESIRED d3方阵 这是一个玩具示例 并且d1 and d2方阵可以是任意维数 这样就实现了类似的功能NA替换品表示赞赏 m1 a1 a2 a3 a4 a5 a
  • 如何清除游戏中心实时排行榜数据?

    是否可以清除实时游戏中心排行榜 而不是沙盒排行榜 的数据 如果无法清除 是否有办法隐藏排行榜不显示 谢谢 转到 iTunesConnect 中的排行榜 并将分数范围更改为超出范围的值 例如 我的一些高尔夫球手发布的成绩远远低于标准杆 我将范
  • 在 Mac OS X 10.10 上以 64 位模式执行 python

    我正在尝试在 64 位模式下执行 python 从这篇文章 如何在 Mac OS X 上强制使用 64 位 python 我检查了 python 都有 32 64 位二进制 gt lipo info which python gt Arch
  • 在 ruby​​ gpgme 中使用密码回调

    我正在使用 ruby gpgme gem 1 0 8 我的密码回调未被调用 def passfunc args fd args last io IO for fd fd w io puts mypassphrase io flush end
  • 回发后在 TextBox 中设置焦点

    我有一个简单的页面 我想根据文本框中的值过滤列表框 两者都在 UpdatePanel 中 这可以正常工作 但是 在回发后 文本框失去了焦点 所以我将焦点设置回 page load 中 然后我注意到光标现在位于文本的开头 而我希望它位于末尾
  • 使用selenium Python滚动到无限加载页面的末尾

    我正在使用 Selenium 从 Twitter 上抓取关注者姓名 并且该页面是无限的 每当我向下滚动时 我都可以看到新的关注者 不知何故 我想转到页面底部 以便我可以scrape所有的追随者 while number 5 driver e
  • 如何在 SML/NJ 中进行按位与运算?

    我正在编写的程序需要它 重复平方来计算 x n 我似乎找不到它的语法 或者是否支持它 它们可在Word8 and Word结构 let open Word8 infix andb orb xorb notb lt lt gt gt gt g
  • async wait 任务空引用异常

    I am getting NullReferenceExceptions on a webforms project I m maintaining The catch is that there is no stacktrace for
  • 解析命令调用的输出

    因此 我尝试从 python 执行 shell 命令 然后将其存储在数组中或直接解析管道 shell 命令 我通过 subprocess 命令管道传输 shell 数据 并使用 print 语句验证输出 它工作得很好 a subproces