簡體   English   中英

Python進度條ValueError:值超出范圍

[英]Python Progress Bar ValueError: Value out of range

我的進度條達到100%,然后拋出錯誤

from progressbar import Percentage, ProgressBar,Bar,ETA

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                            Percentage(), ' ',
                                            ETA()]).start()
            for i,row in enumerate(cursor):

               '''
                do some work here

               ''' 

                pbar.update(i)

這就是我得到的

Traceback (most recent call last):=========================] 100% ETA:  0:00:00
  File "X:\src\dbtest\PymssqlCheck.py", line 27, in <module>
    fiddler.getRows(condetails, dbdetails, 'compliance', 'doctable', '*', '1000')
  File "X:\src\utilities\fiddler.py", line 45, in getRows
    pbar.update(i)
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 271, in update
    raise ValueError('Value out of range')
ValueError: Value out of range

為什么它達到100%然后失敗? 我在用

https://github.com/niltonvolpato/python-progressbar

我甚至試過了

 i=0                                
            for row in cursor:

                ''' do some work here ''' 

                if i < numrows:
                    pbar.update(i)
                    i=i+1

但我仍然得到同樣的錯誤

編輯

我試過Tomasz Jakub Rup回答

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                        Percentage(), ' ',
                                        ETA()])
for row in pbar(cursor):
    ''' do some work here ''' 

我明白了

File "X:\fiddler.py", line 41, in getRows
    for row in pbar(cursor):
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 180, in __next__
    if self.start_time is None: self.start()
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 311, in start
    self.update(0)
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 283, in update
    self.fd.write(self._format_line() + '\r')
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 243, in _format_line
    widgets = ''.join(self._format_widgets())
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 223, in _format_widgets
    widget = format_updatable(widget, self)
  File "X:\Anaconda2\lib\site-packages\progressbar\widgets.py", line 38, in format_updatable
    if hasattr(updatable, 'update'): return updatable.update(pbar)
  File "X:\Anaconda2\lib\site-packages\progressbar\widgets.py", line 184, in update
    return '%3d%%' % pbar.percentage()
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 208, in percentage
    return self.currval * 100.0 / self.maxval
TypeError: unsupported operand type(s) for /: 'float' and 'classobj'

任何想法為什么?

因為進度條默認為100。 如果有N步,則應指定maxval=N

例如:

from progressbar import Percentage, ProgressBar,Bar,ETA

N = 300

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ', Percentage(), ' ', ETA()],
                   maxval=N).start()

for i in range(N+1):
    pbar.update(i)

大衛和托馬斯,你們倆都非常接近。 有效的解決方案是

pbar = ProgressBar(widgets=[Bar('>', '[', ']'), ' ',
                                            Percentage(), ' ',
                                            ETA()],maxval=someMaxValue)
            for row in pbar(cursor):
                ''' do some work '''

progress = ProgressBar(maxval = my.objects.count()或None).start()為我固定

參考: https//github.com/niltonvolpato/python-progressbar/issues/36

嘗試:

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                        Percentage(), ' ',
                                        ETA()])
for row in pbar(cursor.fetchall()):
    ''' do some work here ''' 

在這種情況下,您不需要更新pbar ,也不需要啟動和完成pbar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM