【问题标题】:print progress counter of python in DOS在DOS中打印python的进度计数器
【发布时间】:2013-05-07 15:01:25
【问题描述】:

test_count = 0

while test_count <= 100:
    print test_count
    test_count +=1

目前此计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。

【问题讨论】:

    标签: python python-2.7 python-3.x


    【解决方案1】:

    sys.stdout.flush 中使用\r 回车符。

    import sys
    import time  # for invoking time.sleep(n_seconds) inside loop
    
    counter = 0
    while counter <= 100:
        time.sleep(1)
        counter += 1
        sys.stdout.write("\rTesting (%ss elapsed)" % counter)
        sys.stdout.flush()
    

    【讨论】:

      【解决方案2】:

      使用\r 并在您的打印语句末尾添加, 以不自动写入换行符,如下面的代码所示。

      另外,请参阅python-progressbar 库,了解一些不错的进度条文本实现。

      import time # Added to demonstrate effect
      
      test_count = 0
      
      while test_count <= 100:
          print "\r%3d" % test_count,
          time.sleep(0.1)
          test_count +=1
      

      【讨论】:

        【解决方案3】:

        您应该包括以下声明: 首先在文件开头导入'os'模块:

        import os
        

        然后在循环末尾添加:

        os.system('cls')
        

        希望对你有所帮助:)

        【讨论】:

          猜你喜欢
          • 2023-03-02
          • 1970-01-01
          • 2022-07-15
          • 1970-01-01
          • 1970-01-01
          • 2018-11-18
          • 1970-01-01
          • 2013-11-19
          • 1970-01-01
          相关资源
          最近更新 更多