python中sys.stdout怎么用?
python中sys.stdout怎么用?
偶然在项目编写中发现了sys.stdout的多种好玩用法,常规思想上,只是认为可以实现输出不换行,后来仔细看了下,总结了额外的两个方法,分别为实现实时进度显示以及回车或者退格符使用,带入到项目中使用,相当好玩,要是有需求使用该方法的一些方式,那么接下来,就一起来了解学习下吧。
1、实时进度显示
importsys
foriinrange(10):
sys.stdout.write(str(i))
sys.stdout.flush()
输出结果:
>>>0123456789
2、实现回车或者利用退格符
foriinrange(10):
sys.stdout.write(str(i)+'\r')
sys.stdout.flush()
time.sleep(0.5)
3、制作进度条
foriinrange(10):
sys.stdout.write(str(i+1/1*10)+'%\r')
sys.stdout.flush()
time.sleep(0.5)
上述为大家介绍的几种方式,是比较常见的在项目中使用的,如果感兴趣的话,可以套入使用。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






