Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 搜索多个字符串并打印匹配项_Python - Fatal编程技术网

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

Python 搜索多个字符串并打印匹配项,python,Python,我有一个命令可以打印出这么长的一行,我正在寻找一种方法来搜索3个不同的字符串,当找到一个时,应该打印出来。文本中始终只有3个选项中的一个 b"投标:5.0\r\n计算机:cpu\r\n合同启用:true\r\n合同:null\r\n期限:4.870952844619751\r\n预计成本:'1666666666666666667'\r\n预计费用:'56000000000000'\r\n投标:null\r\nid:a197d3fa-dfb4-11e8-9f77-A6389E7978\r\n最近更

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

b"投标:5.0\r\n计算机:cpu\r\n合同启用:true\r\n合同:null\r\n期限:4.870952844619751\r\n预计成本:'1666666666666666667'\r\n预计费用:'56000000000000'\r\n投标:null\r\nid:a197d3fa-dfb4-11e8-9f77-A6389E7978\r\n最近更新:1541282756.6588786\r\n\r\n名称:'4444'\r\n格式:\r\nframe\u count:1\r\n frames:'1'\r\n output\u path:C:/Users/me/Google Drive/GolemProject/var/media/e/output/4444\r\n resolution:\r\n-222\r\n review:C:\Users\me\AppData\Local\golem\golem\default\rinkby\res\a197d3fa-dfb4-11e8-9f77-a6389e7978\tmp\current\u preview.PNG\r\n\r\n进度:0.00%\r\nresources:/Google\r\nDrive/GolemProject/var/media/e/fa3ee533-2020-45e7-9f5c-5501baa49285/bmw27/bmw27\u cpu.blend\r\n-C:\Users\me\Google Drive\GolemProject\var\media\e\fa3ee533-2020-45e7-9f5c-5501baa49285\bmw27\bmw27\u cpu.blend\r\n状态:等待\r\n任务超时:0:20:00\r\n任务:1\r\n剩余时间:154\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\n\r\n\n\0:40:00\r\n键入:搅拌机\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)

但是它似乎找不到任何东西,因为没有任何东西被打印出来。

对于字符串对象
printstatus

for line in printstatus
不迭代行,而是逐个迭代放置在
行中的每个字符

使用


相反

您在迭代字符,而不是行

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

这很有效,谢谢。有没有什么方法我可以打印出精确的匹配而不是状态部分?我知道我可以再做一行代码来删除这部分,但只是想知道是否有什么聪明的方法?帕特里克,你是一个纯粹的天才!再次有效。非常感谢。我能为你提供一些价值吗,先生?:-)
status: Waiting