zoukankan      html  css  js  c++  java
  • python进度条的实现

    1 # =========知识储备==========
    2 # 进度条,实际就是固定宽度,根据数据传输进度增加字符串长度.
    3 
    4 #先看怎么固定宽度
    5 print('[%-15s]' %'#')           # %s中间加入(-int)设置进度条宽度,后面%是用后面字符串替换%s位置
    6 print(format("[##","<15"),"]")        # 与上面显示相同,设置进度条宽度,左对齐显示
    1 # 增加字符串个数
    2 width=50
    3 progress = 0.3
    4 print(f"[{int(width*progress)*'#'}]")
     1 #=========实现打印进度条函数==========
     2 
     3 def progress(percent,width=50):
     4     if percent >= 1:
     5         percent=1
     6     show_str = ('%%-%ds' % width) % (int(width*percent)*'#')
     7     print('
    [%s] %d%%' %(show_str, 100*percent), end="")    # 
    是将光标移动到行前和end=""一起的作用是实现让进度条一行显示
     8     # %s位置显示固定宽度字符串,%d显示百分进度前面的数字,%%前面的%是对后面的%转义,为了打印最终出一个%
     9     print(format(f"
    [{int(width*percent)*'#'}", f"<{width}"),f"] {percent*100}%", end="")
    10     # format内置函数可以用"<"(左对齐)和">"(右对齐)固定宽度的字符,如果第二个参数不写,print()语句的第二个参数就会紧贴着字符后面显示
    11 progress(0.4)
    1 [#              ]
    2 [##             ]
    3 
    4 [###############]
    5 
    6 [####################                              ] 40%
    7 [####################                              ] 40.0%
  • 相关阅读:
    JavaScript高级程序设计之元素大小
    软件测试面试必备的一些基础理论概念
    golang跨平台编译
    gin shoudBind
    requests
    excelize
    gin获取全部参数
    golang随机数及pipe
    不安全代码只会在使用 /unsafe 编译的情况下出现
    MongoDB 比较运算符 $eq$gt
  • 原文地址:https://www.cnblogs.com/NoteBook3013/p/10420690.html
Copyright © 2011-2022 走看看