Python 进度条 ValueError: 值超出范围

6

我的进度条达到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'

有什么想法吗?

什么是游标?列表?字典? - Tomasz Jakub Rup
我认为这是一个列表。它是用于执行查询的pymssql游标。 - AbtPst
5个回答

3

David和Tomasz,你们俩都很接近。有效的解决方案是:

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

1
干得好!文档有一个错别字 maxval <-max_val!!!谢谢! - layser

2

默认情况下,进度条的值是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)

1

0

尝试:

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

在这种情况下,您不需要更新 pbar,也不需要启动和完成 pbar

我尝试了你的方法,但现在我得到了一个不同的错误。请查看编辑。 - AbtPst
我喜欢通用解决方案。Cursor.fetchall()返回列表。 - Tomasz Jakub Rup

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接