Search code examples
pythonparsingoutput

Parsing Python subprocess.check_output()


I am trying to use and manipulate output from subprocess.check_output() in python but since it is returned byte-by-byte

for line in output:
    # Do stuff

Does not work. Is there a way that I can reconstruct the output to the line formatting that it has when it is printed to stdout? Or what is the best way to search through and use this output?

Thanks in advance!


Solution

  • subprocess.check_output() returns a single string. Use the str.splitlines() method to iterate over individual lines in that string:

    for line in output.splitlines():