Python:如果值==' null'阅读日志时,连续出现多次?

发布于 2025-01-17 15:31:25 字数 999 浏览 0 评论 0原文

读取 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

Reading an android display mode monitor which gives the value of the plugged in resolution. I want the loop to break if it reads "null" multiple times in a row:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深巷少女 2025-01-24 15:31:25

您的代码有点难以阅读,因为省略了缩进,而缩进是 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.

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

注意:我将 z 重命名为 display_mode

Your code is a bit hard to read because the indentation was left out, and indentation is a part of Python syntax.
But here's my best guess for what you're looking for:

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.

My guess is that you would like to re-enter the loop if the display recovers. If that's true, then I recommend that you post some specifics about that in another SO question.

Note: I renamed z to display_mode.

煮茶煮酒煮时光 2025-01-24 15:31:25

上面的示例未显示有效的 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")

Your example above does not show valid Python code.

For your desired result, you can initialize a counter to 0, increment it every time you get null and reset it back to 0 if any other value appears:

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")
神仙妹妹 2025-01-24 15:31:25

如果我很好地理解你的代码,那么当 z 收到空值时,你会尝试打破循环。在Python中,我们使用None来表示空值。

尝试替换这一行:

if z == 'null'

对于这一行:

if z == None

If I understood your code well You're trying to break the loop when z receives null value. In python we use None to represent null values.

Try to replace this line:

if z == 'null'

For this line:

if z == None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文