【问题标题】:Python: How can I get a loop to break if the value == 'null' shows up multiple times in a row while reading a log?Python:如果值 == \'null\' 在读取日志时连续多次出现,我如何才能打破循环?
【发布时间】:2022-12-01 08:23:28
【问题描述】:

读取一个 android 显示模式监视器,它给出了插入分辨率的值。如果连续多次读取“null”,我希望循环中断:

Display Mode:  720p60hz                                                                                       
Display Mode:  720p60hz                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null

BREAK! 

CODE

import time
import subprocess

while True:

z = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
print(f'Display Mode:  {z}')

t1 = time.time()
t2 = time.time()
if z == 'null':


print(f't1 is :{t1}')

else:
continue

if z == 'null'
print(f't2 is :{t2}')
print('i am null')

if t2-t1 > .1:
  
  break

【问题讨论】:

  • 您可以通过以下方式与“null”进行比较:if z is None:
  • @ewong OP 比较的输出是一个字符串
  • @BTables 感谢您的澄清。我的立场得到纠正。

标签: python loops while-loop


【解决方案1】:

您的代码有点难以阅读,因为省略了缩进,而缩进是 Python 语法的一部分。 但这是我对您要查找的内容的最佳猜测:

import time
import subprocess

THRESHOLD_BAD_DISPLAY_READS = 10  # Set this to the number of bad reads in a row that you can tolerate
num_bad_display_reads = 0

while num_bad_display_reads < THRESHOLD_BAD_DISPLAY_READS:
    display_mode = subprocess.getoutput("adb shell cat /sys/class/display/mode")
    time.sleep(.1)
    if display_mode == 'null' or display_mode is None:
        num_bad_display_reads += 1  # Start counting up
    else:
        num_bad_display_reads = 0  # Reset the count
        #print(f'Display Mode:  {display_mode}')  # Uncomment this during debugging

# You have now left the display loop. Feel free to do something else.

我的猜测是,如果显示恢复,您想重新进入循环。如果那是真的,那么我建议您在另一个 SO 问题中发布一些关于它的细节。

笔记: 我将z 重命名为display_mode

【讨论】:

  • 输出应该是一个完整的字符串,所以我相信条件应该类似于if display_mode.split(':')[1].strip() == 'null':
  • @BTables 你可能是对的。我是 Python 人,不是 Android 人,所以我对这里的预期结果了解不够。我的情况模仿了 OP 的行 if z == 'null':
  • 谢谢,对我发布它的方式感到抱歉。老实说,这是我整天浏览谷歌后第一次发布问题。这有帮助,虽然它只显示 0 而不是“显示模式:...”它仍然可以正常工作
  • @OmarN 我修好了。我有一个错误,在显示它之前正在覆盖display_mode
【解决方案2】:

您上面的示例没有显示有效的 Python 代码。

对于您想要的结果,您可以将计数器初始化为 0,每次获得 null 时递增它,如果出现任何其他值则将其重置回 0:

counter = 0

while counter < 5: 
    if value == 'null':
        counter += 1
    else:
        counter = 0

    # Code runs as usual here

print("Too many null values in a row, halting")

【讨论】:

  • 谢谢你的帮助!这也有效,重置值是我被困的地方!
【解决方案3】:

如果我很好地理解您的代码,您正试图在 z 收到空值时打破循环。在 python 中,我们使用 None 来表示空值。

尝试替换此行:

if z == 'null'

对于这一行:

if z == None

【讨论】:

  • OP 的代码正在与字符串进行比较,而不是None。这行不通。
猜你喜欢
  • 2021-08-24
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2017-09-18
  • 1970-01-01
相关资源
最近更新 更多