Python中的命令输出解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16175192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:57:05  来源:igfitidea点击:

Command output parsing in Python

pythonpython-3.x

提问by user2253876

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

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

The output of ipconfig /displaydns"is something like this,

的输出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'

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

从这个输出我感兴趣的值A (Host) RecordRecord Name它们127.0.0.169.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.分别。

How would I do it in Python?

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

回答by Torxed

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'])

gives:

给出:

{'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()

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