Python Forum

Full Version: Best way to iterate through output to get the status of devices
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using python 2.7.5 I currently using subprocess to run a local command and I'm trying to iterate over a list to get the status of each device.

example partial command "status" output: there are about 50 items in the output

name: one

date: blah

status: ok

name: two

date: blah

status: bad

partial code:

import subprocess, os

status = subprocess.Popen('command', 'option1', 'option2', var, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

output, err = status.communicate()

expected output:

one ok

two bad

etc

attempts:

I tried using a for line in output statement with multiple if statements for if 'name' and if 'status' while appending them to a list, but i'm not sure if this is the best way. Any thoughts?