Python中的命令输出解析 [英] Command output parsing in Python

查看:32
本文介绍了Python中的命令输出解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 7 上编写一个 Python 脚本,它读取命令 ipconfig/displaydns 的输出并尝试从输出中获取一些值.

I am trying to write a Python script on Windows 7, which reads the output of command ipconfig /displaydns and try to get some values from the output.

ipconfig/displaydns"的输出是这样的,

Windows IP Configuration

9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.

Record Name . . . . . : 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.
Record Type . . . . . : 1
Time To Live  . . . . : 294
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 127.0.0.16

我正在获取这个输出并将其保存在一个变量中,如下所示,

I am taking this output and saving it in a variable as below,

output = subprocess.check_output("ipconfig /displaydns", shell=True)

当我打印输出"时,我得到以下内容

When I print "output" I get the following

b'\r\nWindows IP Configuration\r\n\r\n   9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.\r\n    ----------------------------------------\r\n    Record Name . . . . . : 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.\r\n    Record Type . . . . . : 1\r\n    Time To Live  . . . . : 289\r\n    Data Length . . . . . : 4\r\n    Section . . . . . . . : Answer\r\n    A (Host) Record . . . : 127.0 .0.16\r\n\r\n\r\n'

从这个输出中,我对 A (Host) RecordRecord Name 的值感兴趣,它们是 127.0.0.16>9.acex-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts..

From this output I am interested in the values for A (Host) Record and Record Name which are 127.0.0.16 and 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts. respectively.

我将如何在 Python 中做到这一点?

How would I do it in Python?

推荐答案

import subprocess
output = subprocess.check_output("ipconfig /displaydns", shell=True)
result = {}
for row in output.split('\n'):
    if ': ' in row:
        key, value = row.split(': ')
        result[key.strip(' .')] = value.strip()

print(result)
print(result['A (Host) Record'])

给出:

{'A (Host) Record': '127.0 .0.16', 'Data Length': '4', 'Section': 'Answer', 'Record Name': '9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.', 'Time To Live': '289', 'Record Type': '1'}
127.0 .0.16


另一种解决方案是:(当我在脑海中想到这一点时,我认为它会更紧凑......它不是,但无论如何,这是一种调用外部命令的不同方式,您可以在其中控制错误和输出(您可以区分两者))


Another solution would be to: (when i thought of this in my head, i thought it would be more compact.. it wasn't but anyway, it's a different way of calling the external command where you get control of the errors and output (you can differntiate the two))

import subprocess
cmdpipe = subprocess.Popen("ipconfig /displaydns", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result = {}
for row in cmdpipe.stdout.readline():
    if ': ' in row:
        key, value = row.split(': ')
        result[key.strip(' .')] = value.strip()

# You need to close the file handles, as they will stay open "forever" otherwise.
cmdpipe.stdout.close()
cmdpipe.stderr.close()

print(result)
print(result['A (Host) Record'])

我还要补充一点,使用 shell=True 可能很危险,尤其是结合使用时与用户输入.它确实增加了一些隐藏的魔法"使某些事情变得更容易或更自然.但在大多数情况下,你会想要执行 subprocess.Popen(["ipconfig", "/displaydns"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 代替.

I'll also add that using shell=True might be dangerous especially when combined with user-input. It does add some "hidden magic" to make some things easier or more user-natural. But in most cases you will want to do subprocess.Popen(["ipconfig", "/displaydns"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) instead.

这篇关于Python中的命令输出解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆