打印父进程和子进程的Python脚本
我有一些父进程和子进程,父进程在第一列,子进程在第二列。
我想把它们打印成下面的格式。期望的输出是:每个子进程前面要加上\t\t,然后再跟上"=>"。
C:\Program Files\Mozilla Thunderbird\thunderbird.exe
=>C:\WINDOWS\System32\WScript.exe E:\s10.vbs
=>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command $codigo =
=>C:\WINDOWS\System32\WScript.exe" "E:\s10.vbs, C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
=>C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
=>\??\C:\WINDOWS\system32\conhost.exe 0xffffffff -ForceV1
我创建了两个列表,它们之间是一一对应的关系,也就是说,父进程的第一个元素对应子进程的第一个元素,依此类推。然后我创建了一个字典,字典的值是空列表,接着把父进程和子进程填充进去。
from collections import defaultdict
from pprint import pprint
parent_proces = [r"C:\Program Files\Mozilla Thunderbird\thunderbird.exe", r"C:\WINDOWS\System32\WScript.exe E:\s10.vbs", r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command $codigo =", r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command $codigo =", r"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -executionpolicy bypass"]
child_process = [r"C:\WINDOWS\System32\WScript.exe E:\s10.vbs", r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command $codigo =", r"\??\C:\WINDOWS\system32\conhost.exe 0xffffffff -ForceV1", r"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -executionpolicy bypass", r"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe"]
tree_dic = defaultdict(list)
for (parent, child) in zip(parent_proces, child_process):
tree_dic[parent].append(child)
pprint(tree_dic)
我得到的输出是下面这样的。
{'C:\\Program Files\\Mozilla Thunderbird\\thunderbird.exe': ['C:\\WINDOWS\\System32\\WScript.exe '
'E:\\s10.vbs'],
'C:\\WINDOWS\\System32\\WScript.exe E:\\s10.vbs': ['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe '
'-command '
'$codigo ='],
'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -windowstyle hidden -executionpolicy bypass': ['C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\RegAsm.exe'],
'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command $codigo =': ['\\??\\C:\\WINDOWS\\system32\\conhost.exe '
'0xffffffff '
'-ForceV1',
'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe '
'-windowstyle '
'hidden '
'-executionpolicy '
'bypass']})
现在我需要把它打印成期望的输出。有没有人能告诉我怎么实现这个逻辑?
1 个回答
1
好的,zip的工作原理是这样的...
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 0]
for _a, _b in zip(a, b):
print(_a, _b)
# will get output like
1 6
2 7
3 8
4 9
5 0
我不太明白你具体需要什么,不过。
parent_l = [...]
child_l = [...]
desired_d = dict()
for I, PARENT in enumerate(parent_l):
desired_d[PARENT] = list()
for CHILD in child_l[I:]:
desired_d[PARENT].append(CHILD)
# Now you can I guess simply print it (or how ever you want)
for I, i in enumerate(desired_d.keys()):
print('\t'*I + i)
编辑:
for I, i in enumerate(desired_d.keys()):
print('\t'*I + f'{i}')