Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQL插入进度条_Python_Python 2.7 - Fatal编程技术网

Python SQL插入进度条

Python SQL插入进度条,python,python-2.7,Python,Python 2.7,我想能够插入一种进度条到这个脚本,因为它需要一段时间来加载 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

我想能够插入一种进度条到这个脚本,因为它需要一段时间来加载

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()

您正在将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重新格式化数据,并使用DB的导入/批量上传机制可能值得一试

我想能够插入一种进度条到这个脚本,因为它需要一段时间来加载

python确实有一个进度条包

一旦安装,这就是您可以使用它的方式

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()
您可以通过使用
maxval
update

来更改更新的速率。您只需
打印((计数*100)/50000+“%”)