{"id":1067112,"date":"2024-12-31T16:32:06","date_gmt":"2024-12-31T08:32:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067112.html"},"modified":"2024-12-31T16:32:10","modified_gmt":"2024-12-31T08:32:10","slug":"python%e9%9b%b6%e5%9f%ba%e7%a1%80%e5%85%a5%e9%97%a8%e5%a6%82%e4%bd%95%e5%8a%a0%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1067112.html","title":{"rendered":"python\u96f6\u57fa\u7840\u5165\u95e8\u5982\u4f55\u52a0\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a5d1b8c0-b746-4961-9cfa-e49783794c67.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u96f6\u57fa\u7840\u5165\u95e8\u5982\u4f55\u52a0\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5728Python\u96f6\u57fa\u7840\u5165\u95e8\u65f6\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4f55\u5b89\u88c5\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\u3002\u5b89\u88c5\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u3001\u5bfc\u5165\u6a21\u5757\u65f6\u53ef\u4ee5\u7528import\u8bed\u53e5\u3001\u4f7f\u7528\u6a21\u5757\u65f6\u9700\u8981\u8c03\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u6216\u7c7b\u3002<\/strong> \u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u201c\u5b89\u88c5\u6a21\u5757\u201d\u8fd9\u4e00\u70b9\u3002\u5b89\u88c5\u6a21\u5757\u662fPython\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5f88\u591a\u529f\u80fd\u548c\u5e93\u90fd\u662f\u901a\u8fc7\u7b2c\u4e09\u65b9\u6a21\u5757\u5b9e\u73b0\u7684\u3002Python\u63d0\u4f9b\u4e86pip\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u8f6f\u4ef6\u5305\u3002\u901a\u8fc7pip\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ecePython Package Index\uff08PyPI\uff09\u4e0a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5404\u79cd\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5de5\u5177\u5b89\u88c5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef\uff08Windows\u7528\u6237\u53ef\u4ee5\u6253\u5f00cmd\uff0cMac\u6216Linux\u7528\u6237\u6253\u5f00\u7ec8\u7aef\uff09\u3002<\/li>\n<li>\u8f93\u5165pip install \u6a21\u5757\u540d\uff08\u4f8b\u5982\uff0cpip install requests\uff09\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/li>\n<li>\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210\uff0c\u5b89\u88c5\u6210\u529f\u540e\uff0c\u4f1a\u663e\u793a\u201cSuccessfully installed \u6a21\u5757\u540d\u201d\u3002<\/li>\n<\/ol>\n<p><p>\u7ee7\u7eed\u8be6\u7ec6\u4ecb\u7ecdPython\u96f6\u57fa\u7840\u5165\u95e8\u5982\u4f55\u52a0\u6a21\u5757\u7684\u5404\u4e2a\u65b9\u9762\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u6a21\u5757<\/strong><\/h2>\n<p><h2>1. \u4f7f\u7528pip\u5de5\u5177\u5b89\u88c5\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u7684pip\u5de5\u5177\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5305\u7ba1\u7406\u5668\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e0b\u8f7d\u3001\u5b89\u88c5\u548c\u7ba1\u7406Python\u7684\u5e93\u548c\u6a21\u5757\u3002\u5b89\u88c5\u6a21\u5757\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install \u6a21\u5757\u540d<\/code>\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>Windows\u7528\u6237\u53ef\u4ee5\u6309\u4e0b<code>Win + R<\/code>\uff0c\u7136\u540e\u8f93\u5165<code>cmd<\/code>\u5e76\u6309\u56de\u8f66\u952e\u3002<\/li>\n<li>Mac\u6216Linux\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u6253\u5f00\u7ec8\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8f93\u5165pip\u5b89\u88c5\u547d\u4ee4<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install \u6a21\u5757\u540d<\/code>\uff0c\u4f8b\u5982\u8981\u5b89\u88c5requests\u5e93\uff0c\u53ef\u4ee5\u8f93\u5165<code>pip install requests<\/code>\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5\u8fc7\u7a0b\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6240\u9700\u7684\u6a21\u5757\uff0c\u6210\u529f\u540e\u7ec8\u7aef\u4f1a\u663e\u793a\u7c7b\u4f3c\u4e8e\u201cSuccessfully installed \u6a21\u5757\u540d\u201d\u7684\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h2>2. \u5b89\u88c5\u6307\u5b9a\u7248\u672c\u7684\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u67d0\u4e2a\u7279\u5b9a\u7248\u672c\u7684\u6a21\u5757\uff0c\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u4f60\u7684\u9879\u76ee\u5bf9\u67d0\u4e2a\u7248\u672c\u6709\u4f9d\u8d56\u3002\u4f7f\u7528pip\u5de5\u5177\u4e5f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u8f93\u5165<code>pip install \u6a21\u5757\u540d==\u7248\u672c\u53f7<\/code>\uff0c\u4f8b\u5982\uff0c\u8981\u5b89\u88c5requests\u5e93\u76842.25.1\u7248\u672c\uff0c\u53ef\u4ee5\u8f93\u5165<code>pip install requests==2.25.1<\/code>\u3002<\/p>\n<\/p>\n<p><h2>3. \u66f4\u65b0\u548c\u5378\u8f7d\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u5b89\u88c5\u6a21\u5757\uff0cpip\u5de5\u5177\u8fd8\u652f\u6301\u66f4\u65b0\u548c\u5378\u8f7d\u6a21\u5757\u3002\u8f93\u5165<code>pip install --upgrade \u6a21\u5757\u540d<\/code>\u53ef\u4ee5\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u7684\u6a21\u5757\uff0c\u4f8b\u5982<code>pip install --upgrade requests<\/code>\u3002\u5982\u679c\u4f60\u9700\u8981\u5378\u8f7d\u67d0\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u8f93\u5165<code>pip uninstall \u6a21\u5757\u540d<\/code>\uff0c\u4f8b\u5982<code>pip uninstall requests<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p><h2>1. \u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u6a21\u5757\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bfc\u5165requests\u6a21\u5757\uff0c\u53ef\u4ee5\u5199<code>import requests<\/code>\u3002\u8fd9\u5c06\u628a\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u5bfc\u5165\u6a21\u5757\u7684\u7279\u5b9a\u90e8\u5206<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ea\u9700\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\uff0c\u53ef\u4ee5\u5199<code>from math import sqrt<\/code>\u3002\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u5199<code>math.sqrt<\/code>\u3002<\/p>\n<\/p>\n<p><h2>3. \u7ed9\u6a21\u5757\u6216\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u8d77\u522b\u540d<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u6216\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u4f60\u53ef\u4ee5\u7ed9\u5bfc\u5165\u7684\u6a21\u5757\u6216\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u8d77\u4e00\u4e2a\u522b\u540d\u3002\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5199<code>import numpy as np<\/code>\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u7528<code>np<\/code>\u6765\u4ee3\u66ff<code>numpy<\/code>\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u6a21\u5757<\/strong><\/h2>\n<p><h2>1. \u8c03\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u6216\u7c7b<\/h2>\n<\/p>\n<p><p>\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u6216\u7c7b\u3002\u4f8b\u5982\uff0c\u5bfc\u5165math\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.sqrt(16)<\/code>\u6765\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\u3002\u5bfc\u5165requests\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests.get(&#39;http:\/\/example.com&#39;)<\/code>\u6765\u53d1\u9001HTTP GET\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h2>2. \u67e5\u770b\u6a21\u5757\u7684\u6587\u6863<\/h2>\n<\/p>\n<p><p>\u5927\u591a\u6570Python\u6a21\u5757\u90fd\u6709\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u6587\u6863\u6765\u4e86\u89e3\u6a21\u5757\u7684\u7528\u6cd5\u548c\u529f\u80fd\u3002\u4f7f\u7528<code>help<\/code>\u51fd\u6570\u6216\u76f4\u63a5\u8bbf\u95ee\u6a21\u5757\u7684\u5b98\u65b9\u7f51\u7ad9\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>help(math)<\/code>\u53ef\u4ee5\u67e5\u770bmath\u6a21\u5757\u7684\u6587\u6863\uff0c\u8bbf\u95ee<code>https:\/\/docs.python-requests.org<\/code>\u53ef\u4ee5\u67e5\u770brequests\u6a21\u5757\u7684\u5728\u7ebf\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h2>3. \u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u529f\u80fd<\/h2>\n<\/p>\n<p><p>\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff0c\u719f\u6089\u8fd9\u4e9b\u5e38\u7528\u529f\u80fd\u53ef\u4ee5\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\u3002\u4f8b\u5982\uff0cmath\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u529f\u80fd\u6709\u5e73\u65b9\u6839\u8ba1\u7b97\uff08sqrt\uff09\u3001\u5e42\u8fd0\u7b97\uff08pow\uff09\u3001\u53d6\u7edd\u5bf9\u503c\uff08fabs\uff09\u7b49\u3002requests\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u529f\u80fd\u6709\u53d1\u9001HTTP\u8bf7\u6c42\uff08get\u3001post\u3001put\u3001delete\u7b49\uff09\u3001\u5904\u7406\u54cd\u5e94\u6570\u636e\uff08json\u3001text\u7b49\uff09\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5e38\u7528\u6a21\u5757\u4ecb\u7ecd<\/strong><\/h2>\n<p><h2>1. requests\u6a21\u5757<\/h2>\n<\/p>\n<p><p>requests\u6a21\u5757\u662fPython\u4e2d\u6700\u5e38\u7528\u7684HTTP\u8bf7\u6c42\u5e93\u4e4b\u4e00\u3002\u5b83\u7b80\u5316\u4e86HTTP\u8bf7\u6c42\u7684\u53d1\u9001\u548c\u5904\u7406\uff0c\u4f7f\u5f97\u4e0eWeb\u670d\u52a1\u7684\u4ea4\u4e92\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong>\uff1a<code>response = requests.get(&#39;http:\/\/example.com&#39;)<\/code><\/li>\n<li><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong>\uff1a<code>response = requests.post(&#39;http:\/\/example.com&#39;, data={&#39;key&#39;: &#39;value&#39;})<\/code><\/li>\n<li><strong>\u5904\u7406\u54cd\u5e94\u6570\u636e<\/strong>\uff1a<code>response.json()<\/code>\uff08\u5c06\u54cd\u5e94\u6570\u636e\u89e3\u6790\u4e3aJSON\u683c\u5f0f\uff09<\/li>\n<\/ul>\n<p><h2>2. numpy\u6a21\u5757<\/h2>\n<\/p>\n<p><p>numpy\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u4ee5\u53ca\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u548c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u521b\u5efa\u6570\u7ec4<\/strong>\uff1a<code>array = np.array([1, 2, 3])<\/code><\/li>\n<li><strong>\u6570\u7ec4\u8fd0\u7b97<\/strong>\uff1a<code>result = np.add(array1, array2)<\/code>\uff08\u6570\u7ec4\u76f8\u52a0\uff09<\/li>\n<li><strong>\u7edf\u8ba1\u51fd\u6570<\/strong>\uff1a<code>mean = np.mean(array)<\/code>\uff08\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff09<\/li>\n<\/ul>\n<p><h2>3. pandas\u6a21\u5757<\/h2>\n<\/p>\n<p><p>pandas\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86DataFrame\u5bf9\u8c61\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u521b\u5efaDataFrame<\/strong>\uff1a<code>df = pd.DataFrame({&#39;col1&#39;: [1, 2], &#39;col2&#39;: [3, 4]})<\/code><\/li>\n<li><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong>\uff1a<code>df = pd.read_csv(&#39;file.csv&#39;)<\/code><\/li>\n<li><strong>\u6570\u636e\u7b5b\u9009\u548c\u5904\u7406<\/strong>\uff1a<code>filtered_df = df[df[&#39;col1&#39;] &gt; 1]<\/code>\uff08\u7b5b\u9009\u51facol1\u5217\u5927\u4e8e1\u7684\u884c\uff09<\/li>\n<\/ul>\n<p><h2>4. matplotlib\u6a21\u5757<\/h2>\n<\/p>\n<p><p>matplotlib\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong>\uff1a<code>plt.plot(x, y)<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>plt.show()<\/code>\u663e\u793a\u56fe\u8868<\/li>\n<li><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong>\uff1a<code>plt.bar(x, y)<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>plt.show()<\/code>\u663e\u793a\u56fe\u8868<\/li>\n<li><strong>\u8bbe\u7f6e\u56fe\u8868\u6807\u9898\u548c\u6807\u7b7e<\/strong>\uff1a<code>plt.title(&#39;Title&#39;)<\/code>\u3001<code>plt.xlabel(&#39;X-axis&#39;)<\/code>\u3001<code>plt.ylabel(&#39;Y-axis&#39;)<\/code><\/li>\n<\/ul>\n<p><h2>5. os\u6a21\u5757<\/h2>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u51fd\u6570\u6765\u6267\u884c\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong>\uff1a<code>current_dir = os.getcwd()<\/code><\/li>\n<li><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong>\uff1a<code>files = os.listdir(directory)<\/code><\/li>\n<li><strong>\u521b\u5efa\u76ee\u5f55<\/strong>\uff1a<code>os.mkdir(directory)<\/code><\/li>\n<li><strong>\u5220\u9664\u6587\u4ef6\u6216\u76ee\u5f55<\/strong>\uff1a<code>os.remove(file_path)<\/code>\u3001<code>os.rmdir(directory)<\/code><\/li>\n<\/ul>\n<h2><strong>\u4e94\u3001\u5b9e\u6218\u7ec3\u4e60<\/strong><\/h2>\n<p><h2>1. \u5b89\u88c5\u548c\u5bfc\u5165requests\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5requests\u6a21\u5757\u3002\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u8f93\u5165<code>pip install requests<\/code>\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6253\u5f00\u4f60\u7684Python\u7f16\u8f91\u5668\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee3\u7801\uff0c\u4f60\u5c06\u770b\u5230\u4ece<a href=\"http:\/\/example.com%E8%BF%94%E5%9B%9E%E7%9A%84HTML%E5%86%85%E5%AE%B9%E3%80%82%E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84HTTP\">http:\/\/example.com\u8fd4\u56de\u7684HTML\u5185\u5bb9\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684HTTP<\/a> GET\u8bf7\u6c42\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528numpy\u548cmatplotlib\u7ed8\u5236\u6b63\u5f26\u6ce2<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5numpy\u548cmatplotlib\u6a21\u5757\u3002\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u8f93\u5165<code>pip install numpy matplotlib<\/code>\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b0\u52302\u03c0\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<h2><strong>\u8ba1\u7b97\u6b63\u5f26\u6ce2<\/strong><\/h2>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u5f26\u6ce2<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;sin(x)&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee3\u7801\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u6b63\u5f26\u6ce2\u56fe\u8868\u3002\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528numpy\u548cmatplotlib\u7ed8\u5236\u6b63\u5f26\u6ce2\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>3. \u4f7f\u7528pandas\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5pandas\u6a21\u5757\u3002\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u8f93\u5165<code>pip install pandas<\/code>\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;column_name&#39;] &gt; value]<\/p>\n<p>print(filtered_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee3\u7801\uff0c\u4f60\u5c06\u770b\u5230\u4ecedata.csv\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u6570\u636e\u4ee5\u53ca\u7b5b\u9009\u540e\u7684\u6570\u636e\u3002\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528pandas\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86Python\u96f6\u57fa\u7840\u5165\u95e8\u5982\u4f55\u52a0\u6a21\u5757\u7684\u5404\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528pip\u5de5\u5177\u5b89\u88c5\u6a21\u5757\u7684\u6b65\u9aa4\uff0c\u5305\u62ec\u5b89\u88c5\u3001\u66f4\u65b0\u548c\u5378\u8f7d\u6a21\u5757\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u4ecb\u7ecd\u4e86\u5bfc\u5165\u6a21\u5757\u7684\u7279\u5b9a\u90e8\u5206\u548c\u7ed9\u6a21\u5757\u6216\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u8d77\u522b\u540d\u7684\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u8c03\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5e76\u67e5\u770b\u6a21\u5757\u7684\u6587\u6863\u3002\u6211\u4eec\u8fd8\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u5e38\u7528\u7684Python\u6a21\u5757\uff0c\u5305\u62ecrequests\u3001numpy\u3001pandas\u3001matplotlib\u548cos\u6a21\u5757\uff0c\u5c55\u793a\u4e86\u5b83\u4eec\u7684\u5e38\u7528\u529f\u80fd\u548c\u7528\u6cd5\u3002\u6700\u540e\uff0c\u901a\u8fc7\u4e00\u4e9b\u5b9e\u6218\u7ec3\u4e60\uff0c\u5e2e\u52a9\u4f60\u8fdb\u4e00\u6b65\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u4f60\u80fd\u591f\u66f4\u52a0\u719f\u7ec3\u5730\u5b89\u88c5\u3001\u5bfc\u5165\u548c\u4f7f\u7528Python\u6a21\u5757\uff0c\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\u548c\u80fd\u529b\u3002Python\u7684\u6a21\u5757\u7cfb\u7edf\u975e\u5e38\u5f3a\u5927\uff0c\u638c\u63e1\u4e86\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\uff0c\u5c06\u4e3a\u4f60\u540e\u7eed\u7684\u5b66\u4e60\u548c\u5f00\u53d1\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6765\u7ec4\u7ec7\u548c\u91cd\u7528\u4ee3\u7801\u3002\u5b89\u88c5\u6a21\u5757\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u8f93\u5165<code>pip install \u6a21\u5757\u540d<\/code>\u6765\u5b89\u88c5\u6240\u9700\u6a21\u5757\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import \u6a21\u5757\u540d<\/code>\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5f15\u7528\u8fd9\u4e2a\u6a21\u5757\uff0c\u4ece\u800c\u4f7f\u7528\u5b83\u63d0\u4f9b\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u63a8\u8350\u7684Python\u6a21\u5757\u9002\u5408\u96f6\u57fa\u7840\u5b66\u4e60\u8005\uff1f<\/strong><br \/>\u5bf9\u4e8e\u96f6\u57fa\u7840\u5b66\u4e60\u8005\uff0c\u5efa\u8bae\u4ece\u4e00\u4e9b\u5e38\u7528\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u6a21\u5757\u5f00\u59cb\uff0c\u4f8b\u5982<code>math<\/code>\uff08\u6570\u5b66\u8fd0\u7b97\uff09\u3001<code>random<\/code>\uff08\u968f\u673a\u6570\u751f\u6210\uff09\u548c<code>datetime<\/code>\uff08\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\uff09\u3002\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u529f\u80fd\uff0c\u5b66\u4e60\u8d77\u6765\u76f8\u5bf9\u7b80\u5355\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u4e0a\u624bPython\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u5b89\u88c5\u6a21\u5757\u65f6\u9047\u5230\u95ee\u9898\uff0c\u6211\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u9047\u5230\u5b89\u88c5\u6a21\u5757\u7684\u95ee\u9898\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u4f60\u7684Python\u73af\u5883\u548cpip\u662f\u5426\u6b63\u786e\u5b89\u88c5\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u8f93\u5165<code>python --version<\/code>\u548c<code>pip --version<\/code>\u6765\u786e\u8ba4\u3002\u5982\u679c\u786e\u8ba4\u65e0\u8bef\u4f46\u4ecd\u7136\u65e0\u6cd5\u5b89\u88c5\u6a21\u5757\uff0c\u8bd5\u7740\u67e5\u770b\u9519\u8bef\u4fe1\u606f\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u9519\u8bef\u4fe1\u606f\u627e\u5230\u89e3\u51b3\u65b9\u6848\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f60\u7684\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u56e0\u4e3a\u5b89\u88c5\u6a21\u5757\u9700\u8981\u4ece\u4e92\u8054\u7f51\u4e0b\u8f7d\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u96f6\u57fa\u7840\u5165\u95e8\u65f6\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4f55\u5b89\u88c5\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\u3002\u5b89\u88c5\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u3001\u5bfc\u5165\u6a21\u5757\u65f6\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1067128,"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\/1067112"}],"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=1067112"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067112\/revisions"}],"predecessor-version":[{"id":1067131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067112\/revisions\/1067131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1067128"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1067112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1067112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1067112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}