0

我希望能够在这个脚本中插入一种进度条,因为它需要一段时间才能加载。

csvReader = csv.reader(open(fo, 'rb'), delimiter=',')
csvList = list(csvReader)
#print len(csvList)
#return
count = 1

for row in csvList[:50000]:
    if count != 1:
        cur.execute(sql2, [row[0], row[2], row[9], row[11], row[18], row[25], row[27], row[15], row[22]])
    count +=1

cnn.close()
4

2 回答 2

1

您正在将 csv 数据加载到内存中 - 所以您有它的长度......

# untested
from itertools import count
rows = len(csvList)
pctn = rows // 100
perc = count()
for rowno, row in enumerate(csvList):
    if rowno % pctn == 0:
        print '{}%'.format(next(perc))

附带说明一下,可能值得考虑让 Python 重新格式化数据,并使用数据库的导入/批量上传机制。

于 2012-06-29T16:17:22.450 回答
0

I want to be able to insert a kind of progress bar to this script as it takes a while to load

python does have a progress-bar package http://code.google.com/p/python-progressbar/

once installed this is how you might use it.

from progressbar import  Bar, ETA, Percentage, ProgressBar, RotatingMarker

csvReader = csv.reader(open(fo, 'rb'), delimiter=',')
csvList = list(csvReader)
#print len(csvList)
#return
count = 1
widgets = ['Parsing CSV: ', Percentage(), ' ', Bar(marker = RotatingMarker()), ' ', ETA()]
pbar = ProgressBar(widgets = widgets, maxval = 50000).start()

for index, row in enumerate(csvList[:50000]):
    if count != 1:
        cur.execute(sql2, [row[0], row[2], row[9], row[11], row[18], row[25], row[27], row[15], row[22]])
    count +=1
    pbar.update(index)

pbar.finish()
cnn.close()

you can change the rate of the update by playing around with maxval and update.

于 2012-06-29T19:04:23.823 回答