Home >Backend Development >Python Tutorial >Detailed example of how to easily implement dynamic progress bar in Python

Detailed example of how to easily implement dynamic progress bar in Python

零到壹度
零到壹度Original
2018-03-31 13:46:453863browse

This article mainly shares with you detailed examples of how to easily implement dynamic progress bars in python. Friends in need can take a look

Ideas and explanations

Assume there is a list of 1,000,000 data (in order to allow the progress bar to be displayed, very large data), each time we loop through the list, we add to the file Output, in which a counter is added, and each time the file is written, the counter is looped through to record, and the final percentage progress displayed is (length of counter/list)*100

Code implementation

#coding = utf-8"""
@author: sy

@file: mooc_stock.py

@time: 2018/3/18 15:01

@desc: 动态显示进度条

"""#coding = utf-8"""
@author: sy

@file: mooc_stock.py

@time: 2018/3/18 15:01

@desc: 动态显示进度条

"""def toolBar():
    #向列表中填充1000000个数据
    list_bar = (range(1000000))    #新增计数器
    count = 0
    #写到桌面文件中
    with open("C:/Users/sy/Desktop/toobar.txt",'w') as f:        for i in list_bar:
            f.write(str(i))
            count = count + 1
            '''
            \r:每次讲控制台的光标移动到首位,去掉则不会呈现刷新的效果,最终是打印一行.
            end='':print输出不换行,若去掉,则会在控制台每次换行打印当前进度.
            '''
            print('\r当前速度:{:.2f}%'.format(count*100/len(list_bar)),end='')if __name__ == '__main__':
    toolBar()

Summary

Percent progress: (length of counter/list)*100

'''
\r:每次讲控制台的光标移动到首位,去掉则不会呈现刷新的效果,最终是打印一行.
end='':print输出不换行,若去掉,则会在控制台每次换行打印当前进度.
'''print('\r当前速度:{:.2f}%'.format(count*100/len(list_bar)),end='')

Related recommendations:

Simple dynamic progress bar implementation

Use JS to implement Dynamic progress bar effect

CSS3 realizes dynamic progress bar

The above is the detailed content of Detailed example of how to easily implement dynamic progress bar in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn