pywinrm 解析输出为 dict 格式 字典格式

s = winrm.Session(host, auth=(username, password))


task_folder = '\RPA\\'
# 运行 schtasks 命令来列出任务计划程序
command = 'schtasks /query /fo csv'
# command = f'schtasks /query /fo csv /tn {task_folder} '
# command = f'schtasks /query /fo csv /tn "{task_folder}\\*"'
# command = f'schtasks /query /fo LIST /tn "{task_folder}\\*"'
result = s.run_cmd(command)

# 将结果返回到本地计算机
print(result.std_out)
# 处理 schtasks 命令的输出,将其转换为 JSON 格式
lines = result.std_out.decode().splitlines()
headers = [header.strip('"') for header in lines[0].split(',')]
tasks = []
for line in lines[1:]:
    task_values = [value.strip('"') for value in line.split(',')]
    task = {header: value for header, value in zip(headers, task_values)}
    tasks.append(task)

# 将结果转换为 JSON 字符串并输出
json_output = json.dumps(tasks)
print(json_output)
# 处理 schtasks 命令的输出,将其转换为字典格式
lines = result.std_out.decode().splitlines()
headers = [header.strip('"') for header in lines[0].split(',')]
tasks = []
for line in lines[1:]:
    task_values = [value.strip('"') for value in line.split(',')]
    task = {header: value for header, value in zip(headers, task_values)}
    tasks.append(task)

# 直接输出字典格式
for task in tasks:
    print(task)
print('len tasks',len(tasks))

你可能感兴趣的:(笔记,python,windows,windows)