{"id":975153,"date":"2024-12-27T06:10:58","date_gmt":"2024-12-26T22:10:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975153.html"},"modified":"2024-12-27T06:11:01","modified_gmt":"2024-12-26T22:11:01","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%8b%e8%bd%bd%e8%bf%9b%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975153.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200534\/a0fe991f-43e2-4f28-a04d-4eda1e6b17b9.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u53ef\u4ee5\u4f7f\u7528ProgressBar\u5e93\u3001Tqdm\u5e93\u3001\u624b\u52a8\u5b9e\u73b0\u8fdb\u5ea6\u6761\u3002<\/strong>\u5176\u4e2d\uff0c<strong>Tqdm\u5e93<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u4e14\u7b80\u5355\u6613\u7528\u7684\u5de5\u5177\uff0c\u5b83\u4e0d\u4ec5\u80fd\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5faa\u73af\u7684\u8fdb\u5ea6\u663e\u793a\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tqdm\u5e93\u6765\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Tqdm\u5e93\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e0b\u8f7d\u6587\u4ef6\u65f6\uff0c\u5c06\u5176\u5305\u88c5\u5728<code>tqdm()<\/code>\u4e2d\u5373\u53ef\u3002Tqdm\u4f1a\u81ea\u52a8\u8ba1\u7b97\u4e0b\u8f7d\u8fdb\u5ea6\uff0c\u5e76\u5728\u63a7\u5236\u53f0\u4e2d\u663e\u793a\u51fa\u6765\u3002Tqdm\u5e93\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u4e0erequests\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u65b9\u4fbf\u5730\u663e\u793a\u6587\u4ef6\u4e0b\u8f7d\u7684\u5b9e\u65f6\u8fdb\u5ea6\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tqdm\u5e93\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tqdm\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Tqdm\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tqdm<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165Tqdm\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tqdm import tqdm<\/p>\n<p>import requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Tqdm\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Tqdm\u5e93\u80fd\u591f\u975e\u5e38\u65b9\u4fbf\u5730\u663e\u793a\u8fdb\u5ea6\u6761\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from tqdm import tqdm<\/p>\n<p>url = &#39;https:\/\/example.com\/largefile.zip&#39;<\/p>\n<p>response = requests.get(url, stream=True)<\/p>\n<p>total_size = int(response.headers.get(&#39;content-length&#39;, 0))<\/p>\n<p>block_size = 1024  # 1 Kibibyte<\/p>\n<p>tqdm_bar = tqdm(total=total_size, unit=&#39;iB&#39;, unit_scale=True)<\/p>\n<p>with open(&#39;largefile.zip&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    for data in response.iter_content(block_size):<\/p>\n<p>        tqdm_bar.update(len(data))<\/p>\n<p>        file.write(data)<\/p>\n<p>tqdm_bar.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790\u4ee3\u7801<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8bf7\u6c42\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>requests.get()<\/code>\u51fd\u6570\u8bf7\u6c42\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\uff0c\u5e76\u5c06<code>stream<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u4ee5\u4fbf\u9010\u5757\u8bfb\u53d6\u6587\u4ef6\u3002<\/li>\n<li><strong>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/strong>\uff1a\u4ece\u54cd\u5e94\u5934\u4e2d\u83b7\u53d6\u6587\u4ef6\u7684\u603b\u5927\u5c0f\uff0c\u5e76\u7528<code>content-length<\/code>\u6765\u786e\u5b9a\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u5757\u5927\u5c0f<\/strong>\uff1a\u5b9a\u4e49\u6bcf\u6b21\u8bfb\u53d6\u7684\u5757\u5927\u5c0f\uff08\u59821KB\uff09\u3002<\/li>\n<li><strong>\u521d\u59cb\u5316\u8fdb\u5ea6\u6761<\/strong>\uff1a\u4f7f\u7528<code>tqdm()<\/code>\u521d\u59cb\u5316\u8fdb\u5ea6\u6761\uff0c\u4f20\u5165\u603b\u5927\u5c0f\uff0c\u5e76\u8bbe\u7f6e\u5355\u4f4d\u548c\u6bd4\u4f8b\u3002<\/li>\n<li><strong>\u66f4\u65b0\u8fdb\u5ea6\u6761<\/strong>\uff1a\u5728\u5faa\u73af\u4e2d\uff0c\u6bcf\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5757\u540e\uff0c\u4f7f\u7528<code>tqdm_bar.update()<\/code>\u66f4\u65b0\u8fdb\u5ea6\u6761\u3002<\/li>\n<li><strong>\u5173\u95ed\u8fdb\u5ea6\u6761<\/strong>\uff1a\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u8c03\u7528<code>tqdm_bar.close()<\/code>\u5173\u95ed\u8fdb\u5ea6\u6761\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528Tqdm\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><h4>1. \u6613\u4e8e\u96c6\u6210<\/h4>\n<\/p>\n<p><p>Tqdm\u80fd\u591f\u8f7b\u677e\u96c6\u6210\u5230Python\u7684\u5404\u7c7b\u5faa\u73af\u548c\u8fed\u4ee3\u4e2d\uff0c\u4e0d\u4ec5\u9650\u4e8e\u4e0b\u8f7d\u8fdb\u5ea6\u6761\u3002\u5176\u7b80\u6d01\u7684API\u8bbe\u8ba1\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u77ed\u65f6\u95f4\u5185\u5bf9\u73b0\u6709\u4ee3\u7801\u8fdb\u884c\u6269\u5c55\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>2. \u4e30\u5bcc\u7684\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8fdb\u5ea6\u663e\u793a\uff0cTqdm\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u5d4c\u5957\u8fdb\u5ea6\u6761\u3001\u591a\u7ebf\u7a0b\u652f\u6301\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u590d\u6742\u7684\u573a\u666f\u4e2d\uff0c\u7075\u6d3b\u5730\u5c55\u793a\u8fdb\u5ea6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>3. \u517c\u5bb9\u6027\u5f3a<\/h4>\n<\/p>\n<p><p>Tqdm\u80fd\u591f\u5f88\u597d\u5730\u517c\u5bb9\u5404\u7c7b\u7ec8\u7aef\u548c\u73af\u5883\uff0c\u652f\u6301\u5728Jupyter Notebook\u4e2d\u4f7f\u7528\uff0c\u8fd8\u80fd\u4e0e\u5e38\u89c1\u7684\u5e76\u884c\u5904\u7406\u5e93\uff08\u5982multiprocessing\uff09\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fdb\u9636\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u663e\u793a\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>Tqdm\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u8fdb\u5ea6\u6761\u7684\u663e\u793a\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>bar_format<\/code>\u53c2\u6570\u8fdb\u884c\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tqdm_bar = tqdm(total=total_size, unit=&#39;iB&#39;, unit_scale=True,<\/p>\n<p>                bar_format=&#39;{l_bar}{bar}| {n_fmt}\/{total_fmt} [{elapsed}&lt;{rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning}, {rate_fmt}]&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>{l_bar}<\/code>\uff1a\u5de6\u4fa7\u8fdb\u5ea6\u4fe1\u606f\u3002<\/li>\n<li><code>{bar}<\/code>\uff1a\u8fdb\u5ea6\u6761\u4e3b\u4f53\u3002<\/li>\n<li><code>{n_fmt}<\/code>\uff1a\u5df2\u7ecf\u5b8c\u6210\u7684\u6570\u91cf\u3002<\/li>\n<li><code>{total_fmt}<\/code>\uff1a\u603b\u6570\u91cf\u3002<\/li>\n<li><code>{elapsed}<\/code>\uff1a\u5df2\u7ecf\u7ecf\u8fc7\u7684\u65f6\u95f4\u3002<\/li>\n<li><code>{remaining}<\/code>\uff1a\u9884\u8ba1\u5269\u4f59\u65f6\u95f4\u3002<\/li>\n<li><code>{rate_fmt}<\/code>\uff1a\u901f\u5ea6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528Tqdm\u7684\u8fdb\u5ea6\u6761\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528Tqdm\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u7ebf\u7a0b\u4e4b\u95f4\u7684\u540c\u6b65\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>from tqdm import tqdm<\/p>\n<p>import requests<\/p>\n<p>url_list = [&#39;https:\/\/example.com\/file1.zip&#39;, &#39;https:\/\/example.com\/file2.zip&#39;]<\/p>\n<p>def download_file(url):<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    total_size = int(response.headers.get(&#39;content-length&#39;, 0))<\/p>\n<p>    block_size = 1024<\/p>\n<p>    with open(url.split(&#39;\/&#39;)[-1], &#39;wb&#39;) as file, tqdm(total=total_size, unit=&#39;iB&#39;, unit_scale=True) as tqdm_bar:<\/p>\n<p>        for data in response.iter_content(block_size):<\/p>\n<p>            tqdm_bar.update(len(data))<\/p>\n<p>            file.write(data)<\/p>\n<p>with ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    executor.map(download_file, url_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Tqdm\u5e93<\/strong>\u5728Python\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u6761\uff0c\u4e0d\u4ec5\u652f\u6301\u57fa\u672c\u7684\u8fdb\u5ea6\u663e\u793a\uff0c\u8fd8\u80fd\u5728\u590d\u6742\u7684\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u901a\u8fc7\u5bf9Tqdm\u7684\u638c\u63e1\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u5927\u5927\u63d0\u5347\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u9664\u4e86Tqdm\uff0cPython\u7684\u751f\u6001\u7cfb\u7edf\u4e2d\u8fd8\u6709\u5176\u4ed6\u8fdb\u5ea6\u663e\u793a\u5de5\u5177\uff0c\u5982ProgressBar\u3001Rich\u7b49\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ddf\u8e2a\u6587\u4ef6\u4e0b\u8f7d\u7684\u8fdb\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u7ed3\u5408<code>tqdm<\/code>\u5e93\u6765\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u65f6\u66f4\u65b0\u4e0b\u8f7d\u8fdb\u5ea6\u6761\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\nfrom tqdm import tqdm\n\nurl = &#39;https:\/\/example.com\/largefile.zip&#39;\nresponse = requests.get(url, stream=True)\ntotal_size = int(response.headers.get(&#39;content-length&#39;, 0))\nblock_size = 1024  # \u6bcf\u6b21\u4e0b\u8f7d1KB\nwith open(&#39;largefile.zip&#39;, &#39;wb&#39;) as file, tqdm(total=total_size, unit=&#39;iB&#39;, unit_scale=True) as bar:\n    for data in response.iter_content(block_size):\n        file.write(data)\n        bar.update(len(data))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u663e\u793a\u4e0b\u8f7d\u8fc7\u7a0b\u4e2d\u7684\u8fdb\u5ea6\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\uff1f<\/strong><br \/>\u9664\u4e86<code>requests<\/code>\u548c<code>tqdm<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>urllib<\/code>\u5e93\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u867d\u7136\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u4e0b\u8f7d\u7684\u5b57\u8282\u6570\u6765\u66f4\u65b0\u8fdb\u5ea6\u3002\u6839\u636e\u4e2a\u4eba\u559c\u597d\u548c\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728\u4e0b\u8f7d\u5927\u6587\u4ef6\u65f6\u5982\u4f55\u4f18\u5316\u8fdb\u5ea6\u663e\u793a\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u8c03\u6574\u6bcf\u6b21\u4e0b\u8f7d\u7684\u6570\u636e\u5757\u5927\u5c0f\uff0c\u9002\u91cf\u589e\u52a0\u5757\u7684\u5927\u5c0f\u53ef\u4ee5\u51cf\u5c11IO\u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u4e0b\u8f7d\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u4e0b\u8f7d\u6280\u672f\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u540c\u65f6\u4e0b\u8f7d\u4e0d\u540c\u90e8\u5206\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u540e\u5408\u5e76\u7ed3\u679c\uff0c\u8fd9\u6837\u4e5f\u80fd\u63d0\u5347\u6548\u7387\u548c\u8fdb\u5ea6\u663e\u793a\u7684\u6d41\u7545\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\u53ef\u4ee5\u4f7f\u7528ProgressBar\u5e93\u3001Tqdm\u5e93\u3001\u624b\u52a8\u5b9e\u73b0\u8fdb\u5ea6\u6761\u3002\u5176\u4e2d\uff0cTqdm\u5e93\u662f [&hellip;]","protected":false},"author":3,"featured_media":975167,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975153"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=975153"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975153\/revisions"}],"predecessor-version":[{"id":975168,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975153\/revisions\/975168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975167"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}