{"id":965517,"date":"2024-12-27T04:38:46","date_gmt":"2024-12-26T20:38:46","guid":{"rendered":""},"modified":"2024-12-27T04:38:48","modified_gmt":"2024-12-26T20:38:48","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e6%96%9c%e6%9d%a0%e5%88%86%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965517.html","title":{"rendered":"python\u5982\u4f55\u7528\u659c\u6760\u5206\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181746\/45f6d257-0a4a-4897-968e-cdb536e7bd8e.webp\" alt=\"python\u5982\u4f55\u7528\u659c\u6760\u5206\u79bb\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u659c\u6760\uff08\/\uff09\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\u3001\u5b57\u7b26\u4e32\u5206\u5272\u7b26\u548c\u6570\u5b66\u8fd0\u7b97\u7b26\u3002\u6700\u5e38\u89c1\u7684\u7528\u9014\u5305\u62ec\u5728\u6587\u4ef6\u8def\u5f84\u4e2d\u4f7f\u7528\u3001\u7528\u4f5c\u9664\u6cd5\u7b26\u53f7\u4ee5\u53ca\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5206\u5272\u5b57\u7b26\u4e32\u3002\u5728\u6587\u4ef6\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7os.path.join()\u51fd\u6570\u6765\u81ea\u52a8\u9002\u5e94\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4ece\u800c\u907f\u514d\u8de8\u5e73\u53f0\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong>\u5728\u6587\u4ef6\u8def\u5f84\u5904\u7406\u4e0a\uff0cPython\u63d0\u4f9b\u4e86os.path\u6a21\u5757\uff0c\u5176os.path.join()\u51fd\u6570\u80fd\u591f\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u8def\u5f84\u5206\u9694\u7b26\u662f\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Linux\u548cmacOS\u7cfb\u7edf\u4e0a\uff0c\u8def\u5f84\u5206\u9694\u7b26\u662f\u6b63\u659c\u6760\uff08\/\uff09\u3002\u4f7f\u7528os.path.join()\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5c06\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u7ec4\u5408\u6210\u5b8c\u6574\u8def\u5f84<\/strong><\/h2>\n<p>directory = &quot;my_folder&quot;<\/p>\n<p>filename = &quot;my_file.txt&quot;<\/p>\n<p>filepath = os.path.join(directory, filename)<\/p>\n<p>print(filepath)  # \u5728Windows\u4e0a\u8f93\u51fa&quot;my_folder\\my_file.txt&quot;\uff0c\u5728Linux\u4e0a\u8f93\u51fa&quot;my_folder\/my_file.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u659c\u6760\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u7684Python\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5904\u7406\u6587\u4ef6\u8def\u5f84\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u8def\u5f84\u5206\u9694\u7b26\u7684\u8981\u6c42\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os.path.join()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>os.path.join()\u51fd\u6570\u662f\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\u7684\u9996\u9009\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u4e2a\u8def\u5f84\u7ec4\u4ef6<\/strong><\/h2>\n<p>path_components = [&quot;home&quot;, &quot;user&quot;, &quot;documents&quot;, &quot;file.txt&quot;]<\/p>\n<h2><strong>\u4f7f\u7528os.path.join\u7ec4\u5408\u8def\u5f84<\/strong><\/h2>\n<p>complete_path = os.path.join(*path_components)<\/p>\n<p>print(complete_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. pathlib\u6a21\u5757\u7684\u8def\u5f84\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u81eaPython 3.4\u8d77\uff0c\u5f15\u5165\u4e86pathlib\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u65b9\u6cd5\u3002Path\u5bf9\u8c61\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aPath\u5bf9\u8c61<\/strong><\/h2>\n<p>path = Path(&quot;home&quot;) \/ &quot;user&quot; \/ &quot;documents&quot; \/ &quot;file.txt&quot;<\/p>\n<p>print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u7b26\u4e32\u5206\u5272\u4e2d\u7684\u659c\u6760\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u659c\u6760\u53ef\u4ee5\u4f5c\u4e3a\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3001URL\u7b49\u5b57\u7b26\u4e32\u65f6\uff0c\u5408\u7406\u4f7f\u7528\u659c\u6760\u5206\u5272\u5b57\u7b26\u4e32\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528split()\u65b9\u6cd5\u5206\u5272\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86split()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u659c\u6760\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &quot;http:\/\/www.example.com\/path\/to\/resource&quot;<\/p>\n<p>parts = url.split(&quot;\/&quot;)<\/p>\n<p>print(parts)  # \u8f93\u51fa\uff1a[&#39;http:&#39;, &#39;&#39;, &#39;www.example.com&#39;, &#39;path&#39;, &#39;to&#39;, &#39;resource&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u66f4\u590d\u6742\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002re\u6a21\u5757\u63d0\u4f9b\u4e86split()\u65b9\u6cd5\uff0c\u7528\u4e8e\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>text = &quot;name1\/name2\/name3&quot;<\/p>\n<p>parts = re.split(r&#39;\/&#39;, text)<\/p>\n<p>print(parts)  # \u8f93\u51fa\uff1a[&#39;name1&#39;, &#39;name2&#39;, &#39;name3&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u5b66\u8fd0\u7b97\u4e2d\u7684\u659c\u6760\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u659c\u6760\uff08\/\uff09\u7528\u4e8e\u8868\u793a\u9664\u6cd5\u8fd0\u7b97\u3002Python\u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u9664\u6cd5\u8fd0\u7b97\uff1a\u666e\u901a\u9664\u6cd5\u548c\u5730\u677f\u9664\u6cd5\uff08\u6574\u9664\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. \u666e\u901a\u9664\u6cd5<\/h4>\n<\/p>\n<p><p>\u666e\u901a\u9664\u6cd5\u4f7f\u7528\u5355\u4e2a\u659c\u6760\uff08\/\uff09\uff0c\u8fd4\u56de\u6d6e\u70b9\u6570\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 10 \/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a3.3333333333333335<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5730\u677f\u9664\u6cd5\uff08\u6574\u9664\uff09<\/h4>\n<\/p>\n<p><p>\u5730\u677f\u9664\u6cd5\u4f7f\u7528\u53cc\u659c\u6760\uff08\/\/\uff09\uff0c\u8fd4\u56de\u820d\u53bb\u5c0f\u6570\u90e8\u5206\u7684\u6574\u6570\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 10 \/\/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u53cd\u659c\u6760\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53cd\u659c\u6760\uff08\\\uff09\u901a\u5e38\u7528\u4e8e\u8868\u793a\u8f6c\u4e49\u5b57\u7b26\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u8f6c\u4e49\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u53cd\u659c\u6760\u53ef\u4ee5\u7528\u4e8e\u63d2\u5165\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26\uff08\\n\uff09\u3001\u5236\u8868\u7b26\uff08\\t\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Line1\\nLine2\\tTabbed&quot;<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u539f\u6837\u4f7f\u7528\u53cd\u659c\u6760\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u5373\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a&#39;r&#39;\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_string = r&quot;C:\\Users\\Name&quot;<\/p>\n<p>print(raw_string)  # \u8f93\u51fa\uff1aC:\\Users\\Name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Python\u4e2d\u7684\u8def\u5f84\u89c4\u8303\u5316<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u4e00\u81f4\u6027\uff0cPython\u63d0\u4f9b\u4e86\u8def\u5f84\u89c4\u8303\u5316\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os.path.normpath()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>os.path.normpath()\u51fd\u6570\u7528\u4e8e\u89c4\u8303\u5316\u8def\u5f84\uff0c\u6d88\u9664\u5197\u4f59\u7684\u5206\u9694\u7b26\u548c\u76f8\u5bf9\u8def\u5f84\u6807\u8bb0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>path = &quot;folder\/\/subfolder\/..\/file.txt&quot;<\/p>\n<p>normalized_path = os.path.normpath(path)<\/p>\n<p>print(normalized_path)  # \u8f93\u51fa\uff1afolder\/file.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pathlib\u6a21\u5757\u7684resolve()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u7684Path\u5bf9\u8c61\u63d0\u4f9b\u4e86resolve()\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u5e76\u89c4\u8303\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&quot;folder\/\/subfolder\/..\/file.txt&quot;)<\/p>\n<p>resolved_path = path.resolve()<\/p>\n<p>print(resolved_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u5f00\u53d1\u8005\u80fd\u591f\u7075\u6d3b\u5730\u4f7f\u7528\u659c\u6760\u6765\u5904\u7406\u8def\u5f84\u3001\u5206\u5272\u5b57\u7b26\u4e32\u4ee5\u53ca\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u548c\u529f\u80fd\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u659c\u6760\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u79bb\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u7ed3\u5408\u659c\u6760\u4f5c\u4e3a\u5206\u9694\u7b26\u6765\u5206\u79bb\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>&quot;a\/b\/c&quot;.split(&#39;\/&#39;)<\/code>\u5c06\u8fd4\u56de<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u5904\u7406\u4ee5\u659c\u6760\u5206\u9694\u7684\u8def\u5f84\u6216\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u5982\u4f55\u5229\u7528\u659c\u6760\u5206\u79bb\u76ee\u5f55\u548c\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>os.path.split()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6587\u4ef6\u8def\u5f84\u5206\u4e3a\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c<code>os.path.split(&#39;\/home\/user\/file.txt&#39;)<\/code>\u4f1a\u8fd4\u56de<code>(&#39;\/home\/user&#39;, &#39;file.txt&#39;)<\/code>\uff0c\u8fd9\u5bf9\u4e8e\u6587\u4ef6\u7ba1\u7406\u548c\u64cd\u4f5c\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u591a\u4e2a\u659c\u6760\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u591a\u4e2a\u8fde\u7eed\u659c\u6760\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u5e76\u7ed3\u5408<code>filter()<\/code>\u51fd\u6570\u6765\u6e05\u7406\u7a7a\u767d\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>filter(None, &quot;a\/\/b\/\/c&quot;.split(&#39;\/&#39;))<\/code>\u5c06\u8fd4\u56de<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5f97\u5230\u7684\u5217\u8868\u4e2d\u6ca1\u6709\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u659c\u6760\uff08\/\uff09\u4f5c\u4e3a\u8def\u5f84\u5206\u9694\u7b26\u3001\u5b57\u7b26\u4e32\u5206\u5272\u7b26\u548c\u6570\u5b66\u8fd0\u7b97\u7b26\u3002\u6700\u5e38\u89c1\u7684\u7528\u9014\u5305\u62ec\u5728\u6587\u4ef6\u8def\u5f84\u4e2d\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":965523,"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\/965517"}],"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=965517"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965517\/revisions"}],"predecessor-version":[{"id":965524,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965517\/revisions\/965524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/965523"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=965517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=965517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=965517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}