Python 3.x 从字符串输出中检索部分信息

Python 3.x 从字符串输出中检索部分信息,python-3.x,string,Python 3.x,String,我使用了一个名为paramiko的库,通过SSH连接到存储服务器,以访问各种详细信息,其中一个是服务器的硬件状态。我已经成功地将数据作为输出,我面临着解析数据以检索电源状态的困难。提取的数据是字符串格式的,我只需要一行。如果你能帮助我,我将非常感激 如果查看输出,我要检索的特定行是: [PS0]:目前,交流良好,直流良好 注意:您在下面看到的输出都是字符串格式 *********************************** Main Chassis status ************

我使用了一个名为paramiko的库,通过SSH连接到存储服务器,以访问各种详细信息,其中一个是服务器的硬件状态。我已经成功地将数据作为输出,我面临着解析数据以检索电源状态的困难。提取的数据是字符串格式的,我只需要一行。如果你能帮助我,我将非常感激

如果查看输出,我要检索的特定行是:

[PS0]:目前,交流良好,直流良好

注意:您在下面看到的输出都是字符串格式

***********************************
Main Chassis status
***********************************
Controller ID:                 0
Second controller installed:   1
Second controller presence:    1
Power Supply information:
   [PS0]: Present, AC Good, DC Good
   [PS0]: +12V Voltage     = 12.34 Volts (Normal)
   [PS0]: +12V Current     = 9.56 Amps
   [PS0]: +12V Power       = 116.75 Watts
   [PS0]: +5V Voltage      = 5.13 Volts (Normal)
   [PS0]: Temp Sensor1     = 30 C (Normal)
   [PS0]: Temp Sensor2     = 29 C (Normal)
   [PS0]: Fan1 Speed       = 4688 RPM (Normal)
   [PS0]: Fan2 Speed       = 4688 RPM (Normal)
   [PS1]: Present, AC Good, DC Good
   [PS1]: +12V Voltage     = 12.34 Volts (Normal)
   [PS1]: +12V Current     = 9.56 Amps
   [PS1]: +12V Power       = 116.75 Watts
   [PS1]: +5V Voltage      = 5.13 Volts (Normal)
   [PS1]: Temp Sensor1     = 29 C (Normal)
   [PS1]: Temp Sensor2     = 30 C (Normal)
   [PS1]: Fan1 Speed       = 4688 RPM (Normal)
   [PS1]: Fan2 Speed       = 4688 RPM (Normal)
Temperature readings: (in degree C)
   [T1 - Front temperature]:    34 (Normal)
   [T2 - Rear temperature]:     33 (Normal)
   [T3 - CPU temperature]:      33 (Normal)
   [T4 - Midplane temperature]: 29 (Normal)
   [T5 - Midplane temperature]: 29 (Normal)
Voltage readings: (in volt)
   [V1]: 1.54 (Normal)
   [V2]: 3.34 (Normal)
   [V3]: 5.13 (Normal)
   [V4]: 12.34 (Normal)
   [V5]: 0.86 (Normal)
   [V6]: 1.06 (Normal)
   [V7]: 3.17 (Normal)
Battery Information:
   Remaining capacity percentage: 92 %
   Estimated backup time:         89.1 Hours
您可以使用Regex(正则表达式)搜索特定格式并使用它。Python有as
re
模块

让上面提到的硬件状态字符串为
hardware\u status

因此,搜索模式的代码如下所示:

import re

ind_hardware_status = hardware_status.split('\n')
for status in ind_hardware_status:
    match = re.search("\s*\[PS0\]: .* AC .* DC .*$", status) # Search Pattern
    if match:
        print(match.group(0).strip())
此代码假定该行中存在交流、直流电