我有一个程序可以通过PowerShell运行WMI查询,并获取输出并将其存储在字符串中,然后进行打印。当我运行它时,我得到的是一小段文字。我可以找出所需的值(名称,SensorType和值)。但是里面还有很多其他垃圾。
b'\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ nName:内存\ r \ nSensorType:Load \ r \ nValue:40.99033 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__派生:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ nName :CPU核心#2 \ r \ nSensorType:温度\ r \ n值:62 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:已用空间\ r \ n SensorType:负载\ r \ n值:92.39651 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Sensor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT: 3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ nName:CPU内核#1 \ r \ nSensorType:温度\ r \ nValue:62 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ nName:CPU DRAM \ r \ nSensorType:Power \ r \ nValue:1.471058 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Sensor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NA空格:\ r \ n__PATH:\ r \ n名称:CPU核心#2 \ r \ nSensorType:负载\ r \ n值:55.46875 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU包\ r \ nSensorType:Power \ r \ nValue:12.19472 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Sensor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ nName:总线速度\ r \ nSensorType:Clock \ r \ nValue:100.0001 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU Total \ r \ nSensorType:Load \ r \ nValue:51.5625 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU Package \ r \ nSensorType:Temperature \ r \ nValue:62 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Sensor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU内核#2 \ r \ nSensorType:时钟\ r \ n值:3100.004 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Se nsor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__派生:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:温度\ r \ nSensorType:温度\ r \ n值:34 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:使用的内存\ r \ nSensorType:数据\ r \ n值:3.240162 \ r \ nPSComputerName: \ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__派生:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:可用内存\ r \ nSensorType:数据\ r \ n值:4 .664536 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:Sensor \ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{ } \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU Core#1 \ r \ nSensorType:时钟\ r \ n值:3100.004 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS :2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH :\ r \ n名称:CPU内核\ r \ nSensorType:电源\ r \ n值:7.794704 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY :\ r \ n__RELPATH:\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU图形\ r \ nSensorType:电源\ r \ n值:2.226472 \ r \ nPSComputerName:\ r \ n \ r \ n__GENUS:2 \ r \ n__CLASS:传感器\ r \ n__SUPERCLASS:\ r \ n__DYNASTY:\ r \ n__RELPATH :\ r \ n__PROPERTY_COUNT:3 \ r \ n__DERIVATION:{} \ r \ n__SERVER:\ r \ n__NAMESPACE:\ r \ n__PATH:\ r \ n名称:CPU Core#1 \ r \ nSensorType:Load \ r \ nValue:47.65625 \ r \ nPSComputerName:\ r \ n \ r \ n \ r \ n \ r \ n'
这是PowerShell脚本,
function NSV(){
$computer = "LocalHost"
$namespace = "root\OpenHardwareMonitor"
$property1 = "Name"
$property2 = "SensorType"
$property3 = "Value"
Get-WmiObject -class Sensor -computername $computer -namespace $namespace -Property $property1,$property2,$property3
}
这是Python脚本,
import subprocess
p = subprocess.Popen(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./TestScript\";", "&NSV"], stdout=subprocess.PIPE)
(output, err) = p.communicate()
print (output)
我对这些东西还很陌生,因此任何帮助将不胜感激! :)
答案 0 :(得分:0)
您可以尝试使用正则表达式,例如:
import re
for match in re.finditer('SensorType', str(yourstring)):
print("%s: %s" % (match.start(), match.group()))
答案 1 :(得分:0)
您可以将二进制文本放入字符串中,在换行符处进行拆分,并过滤以您指定的内容开头的文本:
import subprocess
p = subprocess.Popen(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./TestScript\";", "&NSV"], stdout=subprocess.PIPE)
(output, err) = p.communicate()
data = str(output)
# data =str(b'\r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Memory\r\nSensorType : Load\r\nValue : 40.99033\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #2\r\nSensorType : Temperature\r\nValue : 62\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Used Space\r\nSensorType : Load\r\nValue : 92.39651\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #1\r\nSensorType : Temperature\r\nValue : 62\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU DRAM\r\nSensorType : Power\r\nValue : 1.471058\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #2\r\nSensorType : Load\r\nValue : 55.46875\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Package\r\nSensorType : Power\r\nValue : 12.19472\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Bus Speed\r\nSensorType : Clock\r\nValue : 100.0001\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Total\r\nSensorType : Load\r\nValue : 51.5625\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Package\r\nSensorType : Temperature\r\nValue : 62\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #2\r\nSensorType : Clock\r\nValue : 3100.004\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Temperature\r\nSensorType : Temperature\r\nValue : 34\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Used Memory\r\nSensorType : Data\r\nValue : 3.240162\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : Available Memory\r\nSensorType : Data\r\nValue : 4.664536\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #1\r\nSensorType : Clock\r\nValue : 3100.004\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Cores\r\nSensorType : Power\r\nValue : 7.794704\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Graphics\r\nSensorType : Power\r\nValue : 2.226472\r\nPSComputerName : \r\n\r\n__GENUS : 2\r\n__CLASS : Sensor\r\n__SUPERCLASS : \r\n__DYNASTY : \r\n__RELPATH : \r\n__PROPERTY_COUNT : 3\r\n__DERIVATION : {}\r\n__SERVER : \r\n__NAMESPACE : \r\n__PATH : \r\nName : CPU Core #1\r\nSensorType : Load\r\nValue : 47.65625\r\nPSComputerName : \r\n\r\n\r\n\r\n')
whatIwant = ["Name","SensorType","Value"]
d = [ q for q in data.split("\r\n") if any(q.startswith(k) for k in whatIwant)]
# put every len(whatIwant ) items into one sublist
step = len(whatIwant)
dd = [d[i:i+step] for i in range(0,len(d),step)]
for k in dd:
print(k)
输出:
['Name : Memory', 'SensorType : Load', 'Value : 40.99033']
['Name : CPU Core #2', 'SensorType : Temperature', 'Value : 62']
['Name : Used Space', 'SensorType : Load', 'Value : 92.39651']
['Name : CPU Core #1', 'SensorType : Temperature', 'Value : 62']
['Name : CPU DRAM', 'SensorType : Power', 'Value : 1.471058']
['Name : CPU Core #2', 'SensorType : Load', 'Value : 55.46875']
['Name : CPU Package', 'SensorType : Power', 'Value : 12.19472']
['Name : Bus Speed', 'SensorType : Clock', 'Value : 100.0001']
['Name : CPU Total', 'SensorType : Load', 'Value : 51.5625']
['Name : CPU Package', 'SensorType : Temperature', 'Value : 62']
['Name : CPU Core #2', 'SensorType : Clock', 'Value : 3100.004']
['Name : Temperature', 'SensorType : Temperature', 'Value : 34']
['Name : Used Memory', 'SensorType : Data', 'Value : 3.240162']
['Name : Available Memory', 'SensorType : Data', 'Value : 4.664536']
['Name : CPU Core #1', 'SensorType : Clock', 'Value : 3100.004']
['Name : CPU Cores', 'SensorType : Power', 'Value : 7.794704']
['Name : CPU Graphics', 'SensorType : Power', 'Value : 2.226472']
['Name : CPU Core #1', 'SensorType : Load', 'Value : 47.65625']