Python:循环内仅打印一次 [英] Python: Print only one time inside a loop
本文介绍了Python:循环内仅打印一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要从相机捕获视频的代码.我想使用Python的Logging库在外壳上获取消息或将其导出到文本文件.
I have a code where I want capture a video from a camera. I want to use Logging library of Python to get messages on the shell or export them to a text file.
这是我的代码的一部分,在while循环中,我要打印成功打开的相机
Here is a part of my code where inside the while loop I want to print Camera Opened Successfully
import numpy as np
import cv2
import logging as log
cap = cv2.VideoCapture('5.mpg')
while True:
ret, image = cap.read()
if ret == True:
log.warning('Camera Opened Successfully')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit = 15.0, tileGridSize=(8,8))
gray1 = clahe.apply(gray)
但是我在shell中得到的是这样:
but what I get in the shell is this:
直到我终止正在运行的脚本.关于如何使其仅打印一次的任何想法.
until I terminated the running script. Any idea on how to make it print only once.
推荐答案
import numpy as np
import cv2
import logging as log
cap = cv2.VideoCapture('5.mpg')
hasOpened = False
while True:
ret, image = cap.read()
if ret and not hasOpened:
log.warning('Camera Opened Successfully')
hasOpened = True
如果要在打印后退出循环,请遵循Matt的回答.此选项将在循环中继续,并且仅打印一次.
If you want to break out of the loop after printing, follow Matt's answer. This option will continue in the loop and only print once.
这篇关于Python:循环内仅打印一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文