在python 2中如何从字符串中获取子字符串
我试图从字符串中获取子字符串,但必须是特定的,在python 2中如何从字符串中获取子字符串,python,windows,string,list,python-2.7,Python,Windows,String,List,Python 2.7,我试图从字符串中获取子字符串,但必须是特定的, 如果您使用这个函数并打印它,您将得到一个包含所有当前无响应进程及其信息的长字符串,我需要特定进程的PID r = os.popen('tasklist /FI "STATUS eq Not Responding"').read().strip() print r 例如,如果chrome.exe没有响应,它将在列表中,我希望获得与之关联的PID。我尝试了split()和pop()来挑选我需要的东西,但没有成功 编辑: 我有10多个进程,它们都
如果您使用这个函数并打印它,您将得到一个包含所有当前无响应进程及其信息的长字符串,我需要特定进程的PID
r = os.popen('tasklist /FI "STATUS eq Not Responding"').read().strip()
print r
例如,如果chrome.exe没有响应,它将在列表中,我希望获得与之关联的PID。我尝试了split()
和pop()
来挑选我需要的东西,但没有成功
编辑:我有10多个进程,它们都共享相同的应用程序名称。 我有必要使用PID,并且它属于正确的应用程序。我也不想让我的剧本毁掉一切 因此,简而言之,我需要找到与指定的“process_name”位于同一行的PID,然后只保留该“PID”
希望这是有意义的。我认为有一种方法可以只获取进程pid,但是如果您想知道如何从字符串中提取特定的子字符串,请使用regex 与pid匹配的正则表达式模式:
\w+\.\w+\s+(\d+)\s
输出:
['4024']
基于:
通过让tasklist以CSV形式返回列表,您可以简化您的生活:
C:\>tasklist /FI "STATUS eq Not Responding" /FO CSV /NH
"jusched.exe","3596","Console","1","13,352 K"
"chrome.exe","4760","Console","1","181,088 K"
"chrome.exe","3456","Console","1","119,044 K"
"chrome.exe","2432","Console","1","24,236 K"
"chrome.exe","440","Console","1","36,420 K"
"chrome.exe","4964","Console","1","60,596 K"
"chrome.exe","3608","Console","1","21,924 K"
"chrome.exe","4996","Console","1","22,348 K"
"chrome.exe","2580","Console","1","38,432 K"
"chrome.exe","3312","Console","1","32,756 K"
"chrome.exe","4600","Console","1","36,072 K"
"chrome.exe","4180","Console","1","24,436 K"
"chrome.exe","4320","Console","1","31,152 K"
"chrome.exe","4120","Console","1","22,632 K"
通过csv
模块利用此功能,您现在可以:
>>> r = os.popen('tasklist /FI "STATUS eq Not Responding" /FO CSV')
>>> import csv
>>> reader = csv.DictReader(r, delimiter=',')
>>> rows = list(reader)
>>> rows[0]
{'Session Name': 'Console', 'Mem Usage': '13,352 K', 'PID': '3596', 'Image Name'
: 'jusched.exe', 'Session#': '1'}
>>> rows[0]['PID']
'3596'
我去掉了
/NH
开关(无标题)以从csv模块中获取词典。请发布“包含所有当前无响应进程及其信息的长字符串”,并向我们展示您需要从该字符串中获取的内容。这有帮助吗?工作正常,但是,我似乎无法让它自动从我想要的进程名称中获取PID。我必须指定行的#。假设第7行上的进程名为fun.exe,我只想要与“fun.exe”关联的PID,这怎么办?
C:\>tasklist /FI "STATUS eq Not Responding" /FO CSV /NH
"jusched.exe","3596","Console","1","13,352 K"
"chrome.exe","4760","Console","1","181,088 K"
"chrome.exe","3456","Console","1","119,044 K"
"chrome.exe","2432","Console","1","24,236 K"
"chrome.exe","440","Console","1","36,420 K"
"chrome.exe","4964","Console","1","60,596 K"
"chrome.exe","3608","Console","1","21,924 K"
"chrome.exe","4996","Console","1","22,348 K"
"chrome.exe","2580","Console","1","38,432 K"
"chrome.exe","3312","Console","1","32,756 K"
"chrome.exe","4600","Console","1","36,072 K"
"chrome.exe","4180","Console","1","24,436 K"
"chrome.exe","4320","Console","1","31,152 K"
"chrome.exe","4120","Console","1","22,632 K"
>>> r = os.popen('tasklist /FI "STATUS eq Not Responding" /FO CSV')
>>> import csv
>>> reader = csv.DictReader(r, delimiter=',')
>>> rows = list(reader)
>>> rows[0]
{'Session Name': 'Console', 'Mem Usage': '13,352 K', 'PID': '3596', 'Image Name'
: 'jusched.exe', 'Session#': '1'}
>>> rows[0]['PID']
'3596'