[英]How can I print a list of process names and their values from a dictionary for each process?
I have a function that returns a dictionary of processes here is a snippet...
我有一个 function ,它返回一个进程字典,这里是一个片段......
from win32com.client import GetObject
wmi = GetObject("winmgmts:")
def get_processes():
dictprocesses = {"Caption": [], "Creation Date": []}
colprocs = wmi.ExecQuery("Select * from Win32_Process)
for item in colprocs:
dictprocesses["Caption"].append(item.Caption)
dictprocesses["Creation Date"].append(item.CreationDate)
return dictprocesses
I printed the the dictionary like this...
我像这样打印字典...
d = get_processes()
for key, value in d.items():
for v in value:
print(key, v)
Output for each process:
Caption: <>
Caption: <>
Creation Date: <>
Creation Date: <>
I want to print the dictionary so that it looks like this...
我想打印字典,使它看起来像这样......
First Process...
Caption: <>
Creation Date: <>
Second Process...
Caption: <>
Creation Date: <>
Etc...
In your code, the second loop continues to run for every value in a key, that's why the output.在您的代码中,第二个循环继续为键中的每个值运行,这就是 output 的原因。
One of the ways to get the expected output could be,获得预期 output 的方法之一可能是,
d = get_processes()
temp_len = len(d["caption"])
i = 0
for i in range(temp_len):
for key, value in d.items():
print(key, value[i])
i += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.