Q:

Search for multiple strings and print out match

I have a command that prints out this long line, and i'm looking for a way to search for 3 different strings and when one is found it should be printed. The text will always only have one of the 3 options in it.

b"bid: 5.0\r\ncompute_on: cpu\r\nconcent_enabled: true\r\ncost: null\r\nduration: 4.870952844619751\r\nestimated_cost: '1666666666666666667'\r\nestimated_fee: '56000000000000'\r\nfee: null\r\nid: a197d3fa-dfb4-11e8-9f77-a6389e8e7978\r\nlast_updated: 1541282756.6588786\r\nname: '4444'\r\noptions:\r\n  compositing: false\r\n  format: PNG\r\n  frame_count: 1\r\n  frames: '1'\r\n  output_path: C:/Users/me/Google Drive/GolemProject/var/media/e/output/4444\r\n  resolution:\r\n  - 222\r\n  - 222\r\npreview: C:\Users\me\AppData\Local\golem\golem\default\rinkeby\res\a197d3fa-dfb4-11e8-9f77-a6389e8e7978\tmp\current_preview.PNG\r\nprogress: 0.00 %\r\nresources:\r\n- C:/Users/me/Google Drive/GolemProject/var/media/e/fa3ee533-2020-45e7-9f5c-5501baa49285/bmw27/bmw27_cpu.blend\r\n- C:\Users\me\Google Drive\GolemProject\var\media\e\fa3ee533-2020-45e7-9f5c-5501baa49285\bmw27\bmw27_cpu.blend\r\nstatus: Waiting\r\nsubtask_timeout: 0:20:00\r\nsubtasks: 1\r\ntime_remaining: ???\r\ntime_started: 1541282753.4829328\r\ntimeout: 0:40:00\r\ntype: Blender\r\n\r\n"

My current code looks like this.
status = subprocess.check_output(["golemcli", "tasks", "show", line], shell=True)
findstatus = ['Waiting', 'Finished', 'Timeout']
printstatus = str(status)
for line in printstatus:
    if any(word in line for word in findstatus):
        print(line)

But it doesnt seem like it finds anything because nothing ever gets printed.

A:

You are iterating over characters - not lines. 
status = b"bid: 5.0\r\ncompute_on: cpu\r\nconcent_enabled: true\r\ncost: null\r\nduration: 4.870952844619751\r\nestimated_cost: '1666666666666666667'\r\nestimated_fee: '56000000000000'\r\nfee: null\r\nid: a197d3fa-dfb4-11e8-9f77-a6389e8e7978\r\nlast_updated: 1541282756.6588786\r\nname: '4444'\r\noptions:\r\n compositing: false\r\n format: PNG\r\n frame_count: 1\r\n frames: '1'\r\n output_path: C:/Users/me/Google Drive/GolemProject/var/media/e/output/4444\r\n resolution:\r\n - 222\r\n - 222\r\npreview: C:\Users\me\AppData\Local\golem\golem\default\rinkeby\res\a197d3fa-dfb4-11e8-9f77-a6389e8e7978\tmp\current_preview.PNG\r\nprogress: 0.00 %\r\nresources:\r\n- C:/Users/me/Google Drive/GolemProject/var/media/e/fa3ee533-2020-45e7-9f5c-5501baa49285/bmw27/bmw27_cpu.blend\r\n- C:\Users\me\Google Drive\GolemProject\var\media\e\fa3ee533-2020-45e7-9f5c-5501baa49285\bmw27\bmw27_cpu.blend\r\nstatus: Waiting\r\nsubtask_timeout: 0:20:00\r\nsubtasks: 1\r\ntime_remaining: ???\r\ntime_started: 1541282753.4829328\r\ntimeout: 0:40:00\r\ntype: Blender\r\n\r\n"

findstatus = ['Waiting', 'Finished', 'Timeout']
printstatus = str(status)

# you need to split it here, by literal \r\n - not the special characters
# for carriage return, linefeed \r\n: 

for line in printstatus.split(r"\r\n"):         # split here by _literal_ \\r\\n 
    if any(word in line for word in findstatus):
        print(line)

Alternate way using sets:
findstatus = set([ 'Waiting', 'Finished', 'Timeout'] )
printstatus = str(status)

# you need to split it here, by literal \r\n - not the special characters
# for carriage return, linefeed \r\n: 

for line in printstatus.split(r"\r\n"):         # split here by _literal_ \\r\\n 
    status = set( line.split() ) & findstatus
    if status:
        print(*status) 

Output:
status: Waiting

