Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用正则表达式选择PID值?_Python_Linux_Subprocess - Fatal编程技术网

Python 如何使用正则表达式选择PID值?

Python 如何使用正则表达式选择PID值?,python,linux,subprocess,Python,Linux,Subprocess,我试图提取所有PID值并将它们放在一个单独的列表中,但我无法提取这些值 提取所有PID值并将其放入单独的列表中 要仅提取pid数字,请更改ps命令以使用特定的用户格式 (-o格式-指定用户定义的格式)以限制输出字段 #!/usr/bin/env python3.7 import subprocess import re import os def main(): output=subprocess.check_output(["ps","aux"]) output=outp

我试图提取所有PID值并将它们放在一个单独的列表中,但我无法提取这些值

提取所有PID值并将其放入单独的列表中

要仅提取
pid
数字,请更改
ps
命令以使用特定的用户格式

-o格式
-指定用户定义的格式)以限制输出字段

#!/usr/bin/env python3.7

import subprocess
import re
import os

def main(): 
    output=subprocess.check_output(["ps","aux"])
    output=output.decode()
    print(output)


if __name__=="__main__":
    main()
样本输出:

import subprocess
import os

def main(): 
    output = subprocess.check_output(["ps", "ax", "-o", "pid", "--no-headers"])
    pids = output.decode().split()
    print(pids)


if __name__=="__main__":
    main()
['1', '2', '3', '4', '6', '8', '9', '10', '11', '12', '13', '14', '16', '17',
 '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '30', '31', '32',
 '33', '34', '35', '37', '38', '39', '40', '41', '42', '44', '45', '46', '47',
 '48', '49', '51', '52', '53', '54', '55', '56', '58', '59', '60', '61', '62',
 '63', '65', '66', '67', '68', '69', '70', '72', '73', '74', '75', '76', '77',
 '79', '80', '81', '82', '83', '84', '86', '87', '88', '89', '90', '91', '93',
 '94', '95', '96', '97', '100', '101', '102', '103', '104', '105', '193', '194',
 '195', '199', '200', '202', '205', '206', '209', '210', '211', '212', '213',
 '214', '220', '231', '248', '287', '288', '289', '290', '291', '296', '297',
 '300', '307', '314', '315', '321', '324', '326', '328', '341', '344', '347',
 '348', '357', '361', '362', '363', '366', '432', '483', '488', '494', '516',
 '517', '518', '519', '520', '521', '522', '523', '524', '525', '526', '527',
 '528', '529', '604', '620', '621', '624', '625', '627', '636', '637', '650',
 '651', '743', '744', '752', '753', '770', '771', '785', '786', '791', '792',
 '793', '794', '795', '796', '797', '798', '829', '838', '848', '853', '854',
 '855', '856', '857', '858', '859', '860', '865', '896', '900', '901', '911',
 '912', '921', '936', '937', '940', '944', '960', '964', '968', '970', '975',
 '984', '989', '991', '995', '999', '1001', '1016', '1025', '1030', '1033',
 '1034', '1036', '1038', '1050', '1059', '1067', '1071', '1078', '1095', '1098',
 '1104', '1110', '1112', '1117', '1122', '1131', '1132', '1152', '1157', '1163',
 '1169', '1175', '1181', '1191', '1201', '1204', '1210', '1218', '1225', '1250',
 '1258', '1261', '1288', '1289', '1290', '1291', '1292', '1293', '1294', '1295',
 '1296', '1297', '1298', '1300', '1327', '1334', '1339', '1346', '1395', '1436',
 '1444', '1469', '1682', '1687', '1689', '1701', '1715', '1727', '1751', '1771',
 '1797', '1837', '1900', '1902', '1992', '2025', '2075', '2307', '2492', '2801',
 '2842', '2911', '3404', '3870', '3871', '3874', '4086', '4195', '5217', '5249',
 '5745', '5762', '5773', '5803', '5808', '5809', '5812', '5813', '5816', '5836',
 '5841', '6008', '6073', '6087', '6104', '6605', '7934', '8127', '8663',
 '10274', '10862', '12317', '12428', '12605', '12622', '12650', '12676',
 '12677', '12756', '12904', '13242', '13609', '14722', '14812', '15367',
 '15409', '15522', '15536', '15839', '15859', '16087', '16152', '16303',
 '16386', '16387']