Python:如果值==' null'阅读日志时,连续出现多次?
读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码有点难以阅读,因为省略了缩进,而缩进是 Python 语法的一部分。
但这是我对您正在寻找的内容的最佳猜测:
我的猜测是,如果显示恢复,您希望重新进入循环。如果这是真的,那么我建议您在另一个问题中发布一些有关该问题的细节。
注意:我将
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:
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
todisplay_mode
.上面的示例未显示有效的 Python 代码。
为了获得您想要的结果,您可以将计数器初始化为 0,每次获得 null 时递增它,并在出现任何其他值时将其重置回 0:
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:
如果我很好地理解你的代码,那么当 z 收到空值时,你会尝试打破循环。在Python中,我们使用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:
For this line: