搜索多个字符串并打印出匹配项

时间:2018-11-03 22:28:24

标签: python

我有一条打印出此长行的命令,我正在寻找一种方法来搜索3个不同的字符串,当找到一个字符串时,应该将其打印出来。文本中始终始终只有三个选项之一。

  

b“出价: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 \ n名称:'4444'\ r \ n选项:\ r \ n合成:false \ r \ n格式: PNG \ r \ n frame_count:1 \ r \ n帧:'1'\ r \ n output_path:C:/ Users / me / Google Drive / GolemProject / var / media / e / output / 4444 \ r \ n分辨率: \ r \ n-222 \ r \ n-222 \ r \ n预览:C:\ Users \ me \ AppData \ Local \ golem \ golem \ default \ rinkeby \ res \ a197d3fa-dfb4-11e8-9f77-a6389e8e7978 \ tmp \ current_preview.PNG \ r \ n进度:0.00%\ r \ n资源:\ 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:等待中\ r \ nsubtask_timeout:0:20​​:00 \ r \ nsubtasks:1 \ r \ ntime_remaining:??? \ r \ ntime_started:15412827 53.4829328 \ r \ n超时:0:40:00 \ r \ n类型:Blender \ r \ n \ r \ n“

我当前的代码如下。

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)

但是似乎找不到任何东西,因为什么都没打印。

2 个答案:

答案 0 :(得分:1)

对于字符串对象printstatus

for line in printstatus

不会遍历行,而是遍历放置在line中的每个单个字符。

使用

for line in printstatus.splitlines()

代替

答案 1 :(得分:1)

您正在遍历字符-而不是行。

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)

使用集的替代方式:

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) 

输出:

status: Waiting