{"id":948221,"date":"2024-12-27T00:01:46","date_gmt":"2024-12-26T16:01:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948221.html"},"modified":"2024-12-27T00:01:48","modified_gmt":"2024-12-26T16:01:48","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0parser","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948221.html","title":{"rendered":"python\u5982\u4f55\u6dfb\u52a0parser"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083658\/3a622988-ee8d-46b9-8090-8fb4cabfc978.webp\" alt=\"python\u5982\u4f55\u6dfb\u52a0parser\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6dfb\u52a0\u89e3\u6790\u5668\uff08parser\uff09\u901a\u5e38\u662f\u4e3a\u4e86\u5904\u7406\u548c\u5206\u6790\u67d0\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6216\u547d\u4ee4\u884c\u53c2\u6570\u3002<strong>\u5e38\u7528\u7684\u89e3\u6790\u5668\u5305\u62ecargparse\u7528\u4e8e\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u3001BeautifulSoup\u7528\u4e8eHTML\/XML\u89e3\u6790\u3001\u4ee5\u53caJSON\u89e3\u6790\u5668\u7528\u4e8eJSON\u6570\u636e\u5904\u7406<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528argparse\u5e93\u6765\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>argparse<\/code>\u5e93\u53ef\u4ee5\u4e3aPython\u811a\u672c\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u529f\u80fd\u3002\u8fd9\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002argparse\u7684\u4f7f\u7528\u76f8\u5bf9\u7b80\u5355\uff1a\u9996\u5148\u521b\u5efa\u4e00\u4e2aArgumentParser\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528add_argument\u65b9\u6cd5\u6dfb\u52a0\u53c2\u6570\uff0c\u6700\u540e\u89e3\u6790\u53c2\u6570\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528argparse\uff1a<\/p>\n<\/p>\n<p><h3>ARGPARSE\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>argparse<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6a21\u5757\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u4e3a\u811a\u672c\u6dfb\u52a0\u547d\u4ee4\u884c\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efaArgumentParser\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u521b\u5efaArgumentParser\u5bf9\u8c61\u662f\u4f7f\u7528argparse\u7684\u7b2c\u4e00\u6b65\u3002\u8fd9\u4e2a\u5bf9\u8c61\u8d1f\u8d23\u5305\u542b\u6240\u6709\u5fc5\u8981\u7684\u4fe1\u606f\uff0c\u4ee5\u4fbf\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efaArgumentParser\u5bf9\u8c61<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&quot;\u63cf\u8ff0\u4f60\u7684\u7a0b\u5e8f\u529f\u80fd&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>add_argument<\/code>\u65b9\u6cd5\u4e3a\u4f60\u7684\u7a0b\u5e8f\u5b9a\u4e49\u9700\u8981\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002\u53ef\u4ee5\u6307\u5b9a\u53c2\u6570\u7684\u540d\u79f0\u3001\u7c7b\u578b\u3001\u5e2e\u52a9\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4f4d\u7f6e\u53c2\u6570<\/p>\n<p>parser.add_argument(&quot;filename&quot;, type=str, help=&quot;\u6587\u4ef6\u540d&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u53ef\u9009\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&quot;-v&quot;, &quot;--verbose&quot;, action=&quot;store_true&quot;, help=&quot;\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u89e3\u6790\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>parse_args<\/code>\u65b9\u6cd5\u4f1a\u4ece\u547d\u4ee4\u884c\u4e2d\u89e3\u6790\u51fa\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u53c2\u6570\u540d\u548c\u53c2\u6570\u503c\u7684\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/p>\n<p>args = parser.parse_args()<\/p>\n<h2><strong>\u8bbf\u95ee\u53c2\u6570<\/strong><\/h2>\n<p>print(f&quot;\u6587\u4ef6\u540d: {args.filename}&quot;)<\/p>\n<p>if args.verbose:<\/p>\n<p>    print(&quot;\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>ARGPARSE\u8fdb\u9636\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>argparse\u9664\u4e86\u57fa\u672c\u7684\u53c2\u6570\u89e3\u6790\uff0c\u8fd8\u652f\u6301\u66f4\u590d\u6742\u7684\u7528\u6cd5\uff0c\u5982\u4e92\u65a5\u53c2\u6570\u7ec4\u3001\u5b50\u547d\u4ee4\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e92\u65a5\u53c2\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4e92\u65a5\u53c2\u6570\u7ec4\u662f\u6307\u4e00\u7ec4\u53c2\u6570\u4e2d\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c\u4e0d\u80fd\u540c\u65f6\u6307\u5b9a\u8be6\u7ec6\u548c\u7b80\u7565\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e92\u65a5\u7ec4<\/p>\n<p>group = parser.add_mutually_exclusive_group()<\/p>\n<p>group.add_argument(&quot;-v&quot;, &quot;--verbose&quot;, action=&quot;store_true&quot;, help=&quot;\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f&quot;)<\/p>\n<p>group.add_argument(&quot;-q&quot;, &quot;--quiet&quot;, action=&quot;store_true&quot;, help=&quot;\u5b89\u9759\u6a21\u5f0f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b50\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u80fd\u9700\u8981\u652f\u6301\u5b50\u547d\u4ee4\u3002argparse\u5141\u8bb8\u4e3a\u4e0d\u540c\u7684\u5b50\u547d\u4ee4\u5b9a\u4e49\u4e0d\u540c\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaArgumentParser\u5bf9\u8c61<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;\u4e3b\u7a0b\u5e8f&quot;)<\/p>\n<p>subparsers = parser.add_subparsers(dest=&quot;command&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u547d\u4ee4<\/strong><\/h2>\n<p>parser_a = subparsers.add_parser(&quot;command_a&quot;, help=&quot;\u5b50\u547d\u4ee4A\u7684\u5e2e\u52a9\u4fe1\u606f&quot;)<\/p>\n<p>parser_a.add_argument(&quot;param1&quot;, type=int, help=&quot;\u53c2\u65701&quot;)<\/p>\n<p>parser_b = subparsers.add_parser(&quot;command_b&quot;, help=&quot;\u5b50\u547d\u4ee4B\u7684\u5e2e\u52a9\u4fe1\u606f&quot;)<\/p>\n<p>parser_b.add_argument(&quot;param2&quot;, type=str, help=&quot;\u53c2\u65702&quot;)<\/p>\n<h2><strong>\u89e3\u6790\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>if args.command == &quot;command_a&quot;:<\/p>\n<p>    print(f&quot;\u6267\u884c\u5b50\u547d\u4ee4A\uff0c\u53c2\u65701: {args.param1}&quot;)<\/p>\n<p>elif args.command == &quot;command_b&quot;:<\/p>\n<p>    print(f&quot;\u6267\u884c\u5b50\u547d\u4ee4B\uff0c\u53c2\u65702: {args.param2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5176\u4ed6\u5e38\u7528\u89e3\u6790\u5668<\/h3>\n<\/p>\n<p><p>\u9664\u4e86argparse\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u5e38\u7528\u7684\u89e3\u6790\u5668\uff0c\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. JSON\u89e3\u6790\u5668<\/h4>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790JSON\u6570\u636e\u3002\u5b83\u652f\u6301\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\uff0c\u4ee5\u53ca\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_str = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#39;<\/p>\n<p>data = json.loads(json_str)<\/p>\n<p>print(data[&quot;name&quot;])  # \u8f93\u51fa: Alice<\/p>\n<h2><strong>\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>person = {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30}<\/p>\n<p>json_data = json.dumps(person)<\/p>\n<p>print(json_data)  # \u8f93\u51fa: {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. BeautifulSoup<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u652f\u6301\u591a\u79cd\u89e3\u6790\u5668\uff0c\u5982lxml\u548chtml.parser\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;\u793a\u4f8b\u9875\u9762&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;&lt;p&gt;\u6bb5\u843d\u6587\u672c&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u63d0\u53d6\u6807\u9898<\/strong><\/h2>\n<p>title = soup.title.string<\/p>\n<p>print(title)  # \u8f93\u51fa: \u793a\u4f8b\u9875\u9762<\/p>\n<h2><strong>\u63d0\u53d6\u6bb5\u843d\u6587\u672c<\/strong><\/h2>\n<p>paragraph = soup.p.string<\/p>\n<p>print(paragraph)  # \u8f93\u51fa: \u6bb5\u843d\u6587\u672c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u6dfb\u52a0\u89e3\u6790\u5668\u4e3b\u8981\u662f\u4e3a\u4e86\u5904\u7406\u548c\u5206\u6790\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u636e<\/strong>\u3002<code>argparse<\/code>\u7528\u4e8e\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\uff0c\u975e\u5e38\u9002\u5408\u4e3a\u811a\u672c\u6dfb\u52a0\u547d\u4ee4\u884c\u63a5\u53e3\u3002<code>json<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790\u548c\u7f16\u7801JSON\u6570\u636e\uff0c\u800cBeautifulSoup\u5219\u662fHTML\/XML\u89e3\u6790\u7684\u5229\u5668\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u663e\u8457\u589e\u5f3aPython\u7a0b\u5e8f\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528argparse\u6a21\u5757\u6765\u6dfb\u52a0\u89e3\u6790\u5668\uff1f<\/strong><br \/>argparse\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u7528\u4e8e\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165argparse\u6a21\u5757\uff0c\u521b\u5efaArgumentParser\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528add_argument\u65b9\u6cd5\u6765\u6dfb\u52a0\u89e3\u6790\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import argparse\n\nparser = argparse.ArgumentParser(description=&#39;\u793a\u4f8b\u7a0b\u5e8f&#39;)\nparser.add_argument(&#39;--name&#39;, type=str, help=&#39;\u60a8\u7684\u540d\u5b57&#39;)\nargs = parser.parse_args()\nprint(f&#39;\u4f60\u597d\uff0c{args.name}!&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>--name<\/code>\u7684\u53c2\u6570\uff0c\u5e76\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u5b83\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u53c2\u6570\uff1f<\/strong><br \/>\u4f7f\u7528argparse\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u53c2\u6570\u3002\u901a\u8fc7\u8bbe\u7f6etype\u3001choices\u548crequired\u7b49\u53c2\u6570\uff0c\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7b26\u5408\u9884\u671f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u9650\u5236\u67d0\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7\u7279\u5b9a\u7684\u503c\u6216\u786e\u4fdd\u67d0\u4e2a\u53c2\u6570\u662f\u5fc5\u9700\u7684\u3002  <\/p>\n<pre><code class=\"language-python\">parser.add_argument(&#39;--age&#39;, type=int, choices=range(1, 120), required=True, help=&#39;\u8bf7\u8f93\u5165\u60a8\u7684\u5e74\u9f84&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u5fc5\u987b\u8f93\u5165\u4e00\u4e2a\u4ecb\u4e8e1\u5230119\u4e4b\u95f4\u7684\u6574\u6570\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u63d0\u793a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3aPython\u7684\u89e3\u6790\u5668\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff1f<\/strong><br \/>\u5728argparse\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528default\u53c2\u6570\u4e3a\u89e3\u6790\u5668\u6dfb\u52a0\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u5728\u547d\u4ee4\u884c\u4e2d\u63d0\u4f9b\u8be5\u53c2\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u3002  <\/p>\n<pre><code class=\"language-python\">parser.add_argument(&#39;--language&#39;, type=str, default=&#39;Python&#39;, help=&#39;\u7f16\u7a0b\u8bed\u8a00\uff0c\u9ed8\u8ba4\u4e3aPython&#39;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u672a\u63d0\u4f9b<code>--language<\/code>\u53c2\u6570\uff0c\u7a0b\u5e8f\u5c06\u81ea\u52a8\u4f7f\u7528&#39;Python&#39;\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6dfb\u52a0\u89e3\u6790\u5668\uff08parser\uff09\u901a\u5e38\u662f\u4e3a\u4e86\u5904\u7406\u548c\u5206\u6790\u67d0\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6216\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5e38\u7528\u7684\u89e3\u6790\u5668\u5305\u62ecar [&hellip;]","protected":false},"author":3,"featured_media":948224,"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\/948221"}],"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=948221"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948221\/revisions"}],"predecessor-version":[{"id":948225,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948221\/revisions\/948225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}