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培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
Python sort函数的用法
Python sort函数是Python语言中的一个内置函数,它用于对Python列表进行排序。sort函数可以按照升序或降序的方式来排列列表中的元素,同时还可...详情>>
2023-11-15 21:49:39
capitalize在python中是什么意思
capitalize在Python中是什么意思?在Python中,capitalize()是一个字符串方法,用于将字符串的第一个字符转换为大写字母,而其他字符则保持不变...详情>>
2023-11-15 20:01:18
-t在python中的作用
/t在Python中的作用/t是Python中的一个转义字符,它代表制表符(Tab),用于在字符串中表示水平制表符。在Python中,/t是非常常用的一个字符,...详情>>
2023-11-15 19:10:14
如何使用python3中的heapq模块?
如何使用python3中的heapq模块?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。模块安装:pipinstallheapq模块函数:nlargest()nsm详情>>
2023-11-15 04:29:03