在查看有关Python进度条的以下问题后,我仍然感到困惑: Python Progress Bar
我是Python的新手,我正在尝试向我创建的运行for循环的脚本添加进度条。循环需要花费大量时间来处理,所以我希望进度条显示循环何时运行。令我困惑的部分是,为了实现进度条,我是否已在循环内设置循环?以下是我的代码中的for循环示例:
for member in members:
url = "http://api.wiki123.com/v1.11/member?id="+str(member)
header = {"Authorization": authorization_code}
api_response = requests.get(url, headers=header)
member_check = json.loads(api_response.text)
member_status = member_check.get("response")
我是否需要包含代码以在此循环中添加进度条IN或是否必须包含在循环外部?感谢。
更新:我在这里提到了进度条库,但我对其他库的建议持开放态度。
答案 0 :(得分:0)
一个简单的解决方案是在循环结束时<?php
$server = "localhost";
$user = "root";
$pw = "root";
$db = "uwsTest";
$connect = mysqli_connect($server, $user, $pw, $db);
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo'success!';
}
if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$filesop = fgetcsv($handle, 0, ",");
$coldata = array();
$coldata[ "orderNumber" ] = $filesop[0];
$coldata[ "place" ] = $filesop[1];
$coldata[ "workOrderNum" ] = $filesop[2];
$table_cols = array();
/*staging*/
$table_cols[0] = //200 element array code omitted
$tablenames = array("staging");
for($tableno = 0;$tableno < sizeof($tablenames);$tableno++){
$q = "";
$q2 = "";
$q3 = "";
$q4 = "";
$q5 = "";
$q6 = "";
$col_list = '`'.str_replace(',','`,`',$table_cols[$tableno]).'`';
$q .= "INSERT INTO ".$tablenames[$tableno]." (".$col_list.") VALUES (";
$last_id = mysqli_insert_id($connect);
$cols = explode(",",$table_cols[$tableno]);
$data = array();
foreach($cols as $key => $fldname) {
$data[] = "'".$coldata[$fldname]."'";
}
/*INSERT INTO STAGING TABLE - INITAL CSV UPLOAD*/
$q .= implode(",",$data).");";
/*INSERT INTO CLIENTS TABLE FROM STAGING TABLE*/
$q2 .= "INSERT INTO clients (orderNumber,place,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,address2,servicePreformed)
SELECT orderNumber,place,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,address2,servicePreformed
FROM staging
WHERE StageID = (SELECT MAX(StageID)FROM staging);";
/*INSERT INTO METERS TABLE FROM STAGING TABLE*/
$q3 .= "INSERT INTO meters (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName,bypassSize,meterSize,meterType,manufacturer,registration,technician,linePressurePSI,lat,lon,lowSideRrBefore,highSideRrBefore,firesideRrBefore,lowSideRrAfter,highSideRrAfter,firesideRrAfter,vgOxygen,vgCombustGas,vgCarbonMon,vgHydroSulf)
SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName,bypassSize,meterSize,meterType,manufacturer,registration,technician,linePressurePSI,lat,lon,lowSideRrBefore,highSideRrBefore,firesideRrBefore,lowSideRrAfter,highSideRrAfter,firesideRrAfter,vgOxygen,vgCombustGas,vgCarbonMon,vgHydroSulf
FROM staging
WHERE StageID = (SELECT MAX(StageID)FROM staging);";
/*INSERT INTO TESTS TABLE FROM STAGING TABLE*/
$q4 .= "INSERT INTO tests (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,test1TestRateGPM,test1MeterVol,test1TesterVol,test1Accuracy,test1CorrectAcc,test2TestRateGPM,test2MeterVol,test2TesterVol,test2Accuracy,test2CorrectAcc,test3TestRateGPM,test3MeterVol,test3TesterVol,test3Accuracy,test3CorrectAcc,test4TestRateGPM,test4MeterVol,test4TesterVol,test4Accuracy,test4CorrectAcc,test5TestRateGPM,test5MeterVol,test5TesterVol,test5Accuracy,test5CorrectAcc,test6TestRateGPM,test6MeterVol,test6TesterVol,test6Accuracy,test6CorrectAcc,test7TestRateGPM,test7MeterVol,test7TesterVol,test7Accuracy,test7CorrectAcc,test8TestRateGPM,test8MeterVol,test8TesterVol,test8Accuracy,test8CorrectAcc,inletValveSize,InletValveType,inletValveCond,outletValveSize,outletValveType,outletValveCond,bypassValveSize,bypassValveType,bypassValveCond,vaultLength,vaultWidth,vaultHeight,meterLocation,testPort,testPortInstalled,testPortSize,picture,comments,testResults,retest,test1TestRateGPM2,test1MeterVol2,test1TesterVol2,test1Accuracy2,test1CorrectAcc2,test2TestRateGPM2,test2MeterVol2,test2TesterVol2,test2Accuracy2,test2CorrectAcc2,test3TestRateGPM2,test3MeterVol2,test3TesterVol2,test3Accuracy2,test3CorrectAcc2,test4TestRateGPM2,test4MeterVol2,test4TesterVol2,test4Accuracy2,test4CorrectAcc2,test5TestRateGPM2,test5MeterVol2,test5TesterVol2,test5Accuracy2,test5CorrectAcc2,test6TestRateGPM2,test6MeterVol2,test6TesterVol2,test6Accuracy2,test6CorrectAcc2,test7TestRateGPM2,test7MeterVol2,test7TesterVol2,test7Accuracy2,test7CorrectAcc2,test8TestRateGPM2,test8MeterVol2,test8TesterVol2,test8Accuracy2,test8CorrectAcc2)
SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,test1TestRateGPM,test1MeterVol,test1TesterVol,test1Accuracy,test1CorrectAcc,test2TestRateGPM,test2MeterVol,test2TesterVol,test2Accuracy,test2CorrectAcc,test3TestRateGPM,test3MeterVol,test3TesterVol,test3Accuracy,test3CorrectAcc,test4TestRateGPM,test4MeterVol,test4TesterVol,test4Accuracy,test4CorrectAcc,test5TestRateGPM,test5MeterVol,test5TesterVol,test5Accuracy,test5CorrectAcc,test6TestRateGPM,test6MeterVol,test6TesterVol,test6Accuracy,test6CorrectAcc,test7TestRateGPM,test7MeterVol,test7TesterVol,test7Accuracy,test7CorrectAcc,test8TestRateGPM,test8MeterVol,test8TesterVol,test8Accuracy,test8CorrectAcc,inletValveSize,InletValveType,inletValveCond,outletValveSize,outletValveType,outletValveCond,bypassValveSize,bypassValveType,bypassValveCond,vaultLength,vaultWidth,vaultHeight,meterLocation,testPort,testPortInstalled,testPortSize,picture,comments,testResults,retest,test1TestRateGPM2,test1MeterVol2,test1TesterVol2,test1Accuracy2,test1CorrectAcc2,test2TestRateGPM2,test2MeterVol2,test2TesterVol2,test2Accuracy2,test2CorrectAcc2,test3TestRateGPM2,test3MeterVol2,test3TesterVol2,test3Accuracy2,test3CorrectAcc2,test4TestRateGPM2,test4MeterVol2,test4TesterVol2,test4Accuracy2,test4CorrectAcc2,test5TestRateGPM2,test5MeterVol2,test5TesterVol2,test5Accuracy2,test5CorrectAcc2,test6TestRateGPM2,test6MeterVol2,test6TesterVol2,test6Accuracy2,test6CorrectAcc2,test7TestRateGPM2,test7MeterVol2,test7TesterVol2,test7Accuracy2,test7CorrectAcc2,test8TestRateGPM2,test8MeterVol2,test8TesterVol2,test8Accuracy2,test8CorrectAcc2
FROM staging
WHERE StageID = (SELECT MAX(StageID)FROM staging);";
/*INSERT INTO COSTS TABLE FROM STAGING TABLE*/
$q5 .= "INSERT INTO costs (workOrderNum,onsiteSurveyTestCost,onsiteSurveyTestRepairCost,offsiteSurveyTestCost,offsiteSurveyTestRepairCost,onsiteTestOnlyCost,onsiteTestRepairOnlyCost,onsiteRepairOnly,testPort2,repairCompleteMeterReplacement,repairCompleteMeterReplacementLaborCost,umeCost,umeLaborCost,rotatingLowSideDiskChamber,rotatingLowSideDiskChamberLaborCost,turbineChamberCost,turbineChamberLaborCost,automaticValveCost,automaticValveLaborCost,strainerCost,strainerLaborCost,lowRegisterCost,lowRegisterLaborCost,highRegisterCost,highRegisterLaborCost,miuCost,miuLaborCost,totalCost)
SELECT workOrderNum,onsiteSurveyTestCost,onsiteSurveyTestRepairCost,offsiteSurveyTestCost,offsiteSurveyTestRepairCost,onsiteTestOnlyCost,onsiteTestRepairOnlyCost,onsiteRepairOnly,testPort2,repairCompleteMeterReplacement,repairCompleteMeterReplacementLaborCost,umeCost,umeLaborCost,rotatingLowSideDiskChamber,rotatingLowSideDiskChamberLaborCost,turbineChamberCost,turbineChamberLaborCost,automaticValveCost,automaticValveLaborCost,strainerCost,strainerLaborCost,lowRegisterCost,lowRegisterLaborCost,highRegisterCost,highRegisterLaborCost,miuCost,miuLaborCost,totalCost
FROM staging
WHERE StageID = (SELECT MAX(StageID)FROM staging);";
/*INSERT INTO WORKORDERS TABLE FROM STAGING TABLE*/
$q6 .= "INSERT INTO workorders (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName)
SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName
FROM staging
WHERE StageID = (SELECT MAX(StageID)FROM staging);";
/*DEBUG ALL SQL QUERIES*/
echo "<p>\$q:<pre>".print_r($q,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q2,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q3,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q4,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q5,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q6,true)."</pre></p>\n";
}
/*CONNECT INITAL QUERY AND GET ERROR*/
if(mysqli_multi_query($connect, $q)) {echo'File submitted'; } else { /*var_dump($q)*/echo "Error: " . mysqli_error($connect); }
/*CONNECT OTHER QUERIES - NO ERROR PRINTING*/
mysqli_multi_query($connect,$q2);
mysqli_multi_query($connect,$q3);
mysqli_multi_query($connect,$q4);
mysqli_multi_query($connect,$q5);
mysqli_multi_query($connect,$q6);
}
?>
。这会为for循环的每次迭代打印一个连字符。
如果您想要总是打印出100个连字符,每个'%'连接1个连字符,那么您可以执行类似var ID_COLUMN=0
var URL_COLUMN=1
findings.sort(compareByColumnIndex(URL_COLUMN))
function compareByColumnIndex(index) {
return function(a,b){
if (a[index] === b[index]) {
return 0;
}
else {
return (a[index] < b[index]) ? -1 : 1;
}
}
}
的操作,并额外检查是否为最后一次迭代添加额外的破折号为偶数100。
答案 1 :(得分:0)
使用enlighten库实际上非常容易。只需在循环之前创建一个进度条,然后使用循环的每个实例进行更新。使用管理器,可以为程序的其他部分提供其他进度条。
import enlighten
manager = enlighten.get_manager()
pbar = manager.counter(total=len(members), desc="Checking status", unit='members')
for member in members:
url = "http://api.wiki123.com/v1.11/member?id="+str(member)
header = {"Authorization": authorization_code}
api_response = requests.get(url, headers=header)
member_check = json.loads(api_response.text)
member_status = member_check.get("response")
pbar.update()
答案 2 :(得分:0)
只要代码中的members
有长度,就可以使用atpbar轻松完成。
您只需要用members
包装atpbar
:
from atpbar import atpbar
⋮
for member in atpbar(members):
⋮
答案 3 :(得分:0)
您可以使用 tqdm 库。
你只需要修改你的循环以包含一个计数器,看这个例子:
from tqdm import tqdm
import time
startTime = time.clock()
totalCount = len(members)
for index, member in enumerate(members):
stopTime = time.clock()
statusBarText = tqdm.format_meter(index + 1,
totalCount,
stopTime - startTime,
ncols=80, # prints text 80 characters wide
ascii=False)
print(statusBarText, '\b' * 81, end='')
startTime = time.clock()
... rest of the code in your loop ...