{"id":1156378,"date":"2025-01-13T18:15:18","date_gmt":"2025-01-13T10:15:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156378.html"},"modified":"2025-01-13T18:15:20","modified_gmt":"2025-01-13T10:15:20","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%a4%9a%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156378.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u591a\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194821\/e8249dd2-1e4b-49d1-8ce7-230efaf68994.webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u591a\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u8f93\u5165\u591a\u6570\u636e\uff1a\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41sys.stdin\u3001\u4f7f\u7528GUI\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5148\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528Python\u7684\u5185\u7f6eopen()\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><p>\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u636e\u6587\u4ef6 data.txt\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<h2><strong>1,2,3<\/strong><\/h2>\n<h2><strong>4,5,6<\/strong><\/h2>\n<h2><strong>7,8,9<\/strong><\/h2>\n<h2><strong>\u4f7f\u7528Python\u8bfb\u53d6\u8be5\u6587\u4ef6\u5e76\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>data_file = &#39;data.txt&#39;<\/p>\n<p>with open(data_file, &#39;r&#39;) as file:<\/p>\n<p>    data = file.readlines()<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>for line in data:<\/p>\n<p>    numbers = line.strip().split(&#39;,&#39;)<\/p>\n<p>    numbers = [int(num) for num in numbers]<\/p>\n<p>    print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u5e76\u5904\u7406\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u7684\u591a\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528input()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528input()\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u7528\u6237\u4ece\u63a7\u5236\u53f0\u8f93\u5165\u591a\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u9010\u884c\u8f93\u5165\u6570\u636e<\/li>\n<li>\u4e00\u6b21\u6027\u8f93\u5165\u6240\u6709\u6570\u636e<\/li>\n<\/ol>\n<p><h4>1.\u9010\u884c\u8f93\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9010\u884c\u8f93\u5165\u6570\u636e\u662f\u6307\u7528\u6237\u6309\u884c\u8f93\u5165\u6570\u636e\uff0c\u6bcf\u884c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7528\u6237\u9010\u884c\u8f93\u5165\u6570\u636e\uff0c\u8f93\u5165\u7a7a\u884c\u8868\u793a\u7ed3\u675f<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff0c\u6bcf\u884c\u4e00\u4e2a\u8bb0\u5f55\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f\uff1a&quot;)<\/p>\n<p>data = []<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    data.append(line)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u9010\u884c\u8f93\u5165\u6570\u636e\uff0c\u5f53\u8f93\u5165\u4e00\u4e2a\u7a7a\u884c\u65f6\uff0c\u8f93\u5165\u7ed3\u675f\uff0c\u6240\u6709\u6570\u636e\u88ab\u5b58\u50a8\u5728\u5217\u8868<code>data<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2.\u4e00\u6b21\u6027\u8f93\u5165\u6240\u6709\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e00\u6b21\u6027\u8f93\u5165\u6240\u6709\u6570\u636e\u662f\u6307\u7528\u6237\u5728\u4e00\u884c\u5185\u8f93\u5165\u6240\u6709\u6570\u636e\uff0c\u5e76\u7528\u7279\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7528\u6237\u4e00\u6b21\u6027\u8f93\u5165\u6240\u6709\u6570\u636e\uff0c\u4ee5\u7a7a\u683c\u5206\u9694<\/p>\n<p>data = input(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff0c\u4ee5\u7a7a\u683c\u5206\u9694\uff1a&quot;)<\/p>\n<p>data_list = data.split()<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u4e00\u6b21\u6027\u8f93\u5165\u6240\u6709\u6570\u636e\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694\uff0c\u8f93\u5165\u7684\u6570\u636e\u88ab\u5b58\u50a8\u5728\u5217\u8868<code>data_list<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u9ad8\u6548\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/li>\n<li>\u8bfb\u53d6CSV\u6587\u4ef6<\/li>\n<li>\u8bfb\u53d6JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p><h4>1.\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u662f\u6700\u57fa\u672c\u7684\u6587\u4ef6\u8f93\u5165\u65b9\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728 data.txt \u6587\u4ef6\u4e2d<\/p>\n<p>data_file = &#39;data.txt&#39;<\/p>\n<p>with open(data_file, &#39;r&#39;) as file:<\/p>\n<p>    data = file.readlines()<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>for line in data:<\/p>\n<p>    numbers = line.strip().split(&#39;,&#39;)<\/p>\n<p>    numbers = [int(num) for num in numbers]<\/p>\n<p>    print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728<code>data.txt<\/code>\u6587\u4ef6\u4e2d\uff0c\u7a0b\u5e8f\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u9010\u884c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728 data.csv \u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>data_file = &#39;data.csv&#39;<\/p>\n<p>with open(data_file, &#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.reader(file)<\/p>\n<p>    data = list(csv_reader)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>for row in data:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728<code>data.csv<\/code>\u6587\u4ef6\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u9010\u884c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3.\u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>JSON\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728 data.json \u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>data_file = &#39;data.json&#39;<\/p>\n<p>with open(data_file, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728<code>data.json<\/code>\u6587\u4ef6\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u53ef\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u4f20\u9012\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528sys.argv\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/li>\n<li>\u4f7f\u7528argparse\u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528sys.argv\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>sys.argv<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5217\u8868\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>args = sys.argv[1:]<\/p>\n<p>print(&quot;\u547d\u4ee4\u884c\u53c2\u6570\uff1a&quot;, args)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528argparse\u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>argparse<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u529f\u80fd\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/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;\u547d\u4ee4\u884c\u53c2\u6570\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;numbers&#39;, metavar=&#39;N&#39;, type=int, nargs=&#39;+&#39;, help=&#39;\u8f93\u5165\u7684\u6570\u5b57&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>print(&quot;\u547d\u4ee4\u884c\u53c2\u6570\uff1a&quot;, args.numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41sys.stdin<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41sys.stdin\u53ef\u4ee5\u8bfb\u53d6\u8f93\u5165\u6d41\u4e2d\u7684\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u9010\u884c\u8bfb\u53d6\u6570\u636e<\/li>\n<li>\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e<\/li>\n<\/ol>\n<p><h4>1.\u9010\u884c\u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u6570\u636e\u662f\u6307\u4ece\u6807\u51c6\u8f93\u5165\u6d41\u4e2d\u9010\u884c\u8bfb\u53d6\u6570\u636e\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff0c\u6bcf\u884c\u4e00\u4e2a\u8bb0\u5f55\uff0c\u8f93\u5165Ctrl+D\u7ed3\u675f\uff1a&quot;)<\/p>\n<p>data = sys.stdin.readlines()<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u6807\u51c6\u8f93\u5165\u6d41\u4e2d\u9010\u884c\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868<code>data<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2.\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e\u662f\u6307\u4ece\u6807\u51c6\u8f93\u5165\u6d41\u4e2d\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff0c\u8f93\u5165Ctrl+D\u7ed3\u675f\uff1a&quot;)<\/p>\n<p>data = sys.stdin.read()<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u6807\u51c6\u8f93\u5165\u6d41\u4e2d\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5b57\u7b26\u4e32<code>data<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528GUI\u5e93<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528GUI\u5e93\u53ef\u4ee5\u901a\u8fc7\u56fe\u5f62\u7528\u6237\u754c\u9762\u8f93\u5165\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Tkinter\u5e93<\/li>\n<li>\u4f7f\u7528PyQt\u5e93<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528Tkinter\u5e93<\/h4>\n<\/p>\n<p><p><code>Tkinter<\/code>\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<h2><strong>\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = simpledialog.askstring(&quot;\u8f93\u5165\u6570\u636e&quot;, &quot;\u8bf7\u8f93\u5165\u6570\u636e\uff1a&quot;)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528<code>Tkinter<\/code>\u5e93\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5e76\u901a\u8fc7\u5bf9\u8bdd\u6846\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528PyQt\u5e93<\/h4>\n<\/p>\n<p><p><code>PyQt<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684GUI\u5e93\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QInputDialog<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>data, ok = QInputDialog.getText(None, &quot;\u8f93\u5165\u6570\u636e&quot;, &quot;\u8bf7\u8f93\u5165\u6570\u636e\uff1a&quot;)<\/p>\n<p>if ok:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u53d6\u6d88\u8f93\u5165&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528<code>PyQt<\/code>\u5e93\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5e76\u901a\u8fc7\u8f93\u5165\u5bf9\u8bdd\u6846\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7f51\u7edc\u8f93\u5165<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7f51\u7edc\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u63a5\u53e3\u63a5\u6536\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528HTTP\u8bf7\u6c42<\/li>\n<li>\u4f7f\u7528WebSocket<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528HTTP\u8bf7\u6c42\u53ef\u4ee5\u901a\u8fc7Web API\u63a5\u6536\u6570\u636e\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001HTTP GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<h2><strong>\u5904\u7406\u54cd\u5e94\u6570\u636e<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    data = response.json()<\/p>\n<p>    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u901a\u8fc7\u53d1\u9001HTTP GET\u8bf7\u6c42\u4eceWeb API\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528WebSocket<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528WebSocket\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u4f20\u8f93\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import websocket<\/p>\n<p>def on_message(ws, message):<\/p>\n<p>    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e\uff1a&quot;, message)<\/p>\n<p>def on_error(ws, error):<\/p>\n<p>    print(&quot;\u9519\u8bef\uff1a&quot;, error)<\/p>\n<p>def on_close(ws):<\/p>\n<p>    print(&quot;\u8fde\u63a5\u5173\u95ed&quot;)<\/p>\n<p>def on_open(ws):<\/p>\n<p>    print(&quot;\u8fde\u63a5\u6253\u5f00&quot;)<\/p>\n<h2><strong>\u521b\u5efaWebSocket\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>ws = websocket.WebSocketApp(&quot;ws:\/\/example.com\/websocket&quot;,<\/p>\n<p>                            on_message=on_message,<\/p>\n<p>                            on_error=on_error,<\/p>\n<p>                            on_close=on_close)<\/p>\n<p>ws.on_open = on_open<\/p>\n<h2><strong>\u8fd0\u884cWebSocket\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>ws.run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u901a\u8fc7WebSocket\u5ba2\u6237\u7aef\u63a5\u6536\u5b9e\u65f6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u6570\u636e\u5e93\u8f93\u5165<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6570\u636e\u5e93\u8f93\u5165\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528SQLite<\/li>\n<li>\u4f7f\u7528MySQL<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528SQLite<\/h4>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM data&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = cursor.fetchall()<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceSQLite\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528MySQL<\/h4>\n<\/p>\n<p><p>MySQL\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;example_db&#39;<\/p>\n<p>)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM data&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = cursor.fetchall()<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceMySQL\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u6d88\u606f\u961f\u5217<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6d88\u606f\u961f\u5217\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6570\u636e\u4f20\u8f93\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528RabbitMQ<\/li>\n<li>\u4f7f\u7528Kafka<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528RabbitMQ<\/h4>\n<\/p>\n<p><p>RabbitMQ\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<h2><strong>\u8fde\u63a5\u5230RabbitMQ\u670d\u52a1\u5668<\/strong><\/h2>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<h2><strong>\u58f0\u660e\u961f\u5217<\/strong><\/h2>\n<p>channel.queue_declare(queue=&#39;hello&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>def callback(ch, method, properties, body):<\/p>\n<p>    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e\uff1a&quot;, body)<\/p>\n<h2><strong>\u6d88\u8d39\u6d88\u606f<\/strong><\/h2>\n<p>channel.basic_consume(queue=&#39;hello&#39;, on_message_callback=callback, auto_ack=True)<\/p>\n<p>print(&#39;\u7b49\u5f85\u6570\u636e\u8f93\u5165\u3002\u6309Ctrl+C\u9000\u51fa\u3002&#39;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceRabbitMQ\u961f\u5217\u4e2d\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528Kafka<\/h4>\n<\/p>\n<p><p>Kafka\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u6d41\u5904\u7406\u5e73\u53f0\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kafka import KafkaConsumer<\/p>\n<h2><strong>\u8fde\u63a5\u5230Kafka\u670d\u52a1\u5668<\/strong><\/h2>\n<p>consumer = KafkaConsumer(&#39;example_topic&#39;, bootstrap_servers=[&#39;localhost:9092&#39;])<\/p>\n<h2><strong>\u63a5\u6536\u6d88\u606f<\/strong><\/h2>\n<p>for message in consumer:<\/p>\n<p>    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e\uff1a&quot;, message.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceKafka\u4e3b\u9898\u4e2d\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6d41\u5f0f\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6d41\u5f0f\u6570\u636e\u5904\u7406\u53ef\u4ee5\u5904\u7406\u5b9e\u65f6\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Apache Spark<\/li>\n<li>\u4f7f\u7528Flink<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528Apache Spark<\/h4>\n<\/p>\n<p><p>Apache Spark\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5206\u5e03\u5f0f\u6570\u636e\u5904\u7406\u6846\u67b6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyspark.sql import SparkSession<\/p>\n<h2><strong>\u521b\u5efaSparkSession<\/strong><\/h2>\n<p>spark = SparkSession.builder.appName(&quot;StreamDataProcessing&quot;).getOrCreate()<\/p>\n<h2><strong>\u8bfb\u53d6\u6d41\u6570\u636e<\/strong><\/h2>\n<p>df = spark.readStream.format(&quot;socket&quot;).option(&quot;host&quot;, &quot;localhost&quot;).option(&quot;port&quot;, 9999).load()<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>df.printSchema()<\/p>\n<h2><strong>\u542f\u52a8\u67e5\u8be2<\/strong><\/h2>\n<p>query = df.writeStream.outputMode(&quot;append&quot;).format(&quot;console&quot;).start()<\/p>\n<h2><strong>\u7b49\u5f85\u67e5\u8be2\u7ed3\u675f<\/strong><\/h2>\n<p>query.aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tTermination()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528Apache Spark\u5904\u7406\u6d41\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528Flink<\/h4>\n<\/p>\n<p><p>Flink\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6d41\u5904\u7406\u6846\u67b6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyflink.datastream import StreamExecutionEnvironment<\/p>\n<p>from pyflink.datastream.connectors import FlinkKafkaConsumer<\/p>\n<p>from pyflink.common.serialization import SimpleStringSchema<\/p>\n<h2><strong>\u521b\u5efa\u6267\u884c\u73af\u5883<\/strong><\/h2>\n<p>env = StreamExecutionEnvironment.get_execution_environment()<\/p>\n<h2><strong>\u521b\u5efaKafka\u6d88\u8d39\u8005<\/strong><\/h2>\n<p>consumer = FlinkKafkaConsumer(<\/p>\n<p>    topics=&#39;example_topic&#39;,<\/p>\n<p>    deserialization_schema=SimpleStringSchema(),<\/p>\n<p>    properties={&#39;bootstrap.servers&#39;: &#39;localhost:9092&#39;}<\/p>\n<p>)<\/p>\n<h2><strong>\u8bfb\u53d6\u6d41\u6570\u636e<\/strong><\/h2>\n<p>data_stream = env.add_source(consumer)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>data_stream.print()<\/p>\n<h2><strong>\u542f\u52a8\u6267\u884c\u73af\u5883<\/strong><\/h2>\n<p>env.execute(&quot;StreamDataProcessing&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528Flink\u5904\u7406\u6d41\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u8f93\u5165\u7684\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u591a\u7ebf\u7a0b<\/li>\n<li>\u4f7f\u7528\u591a\u8fdb\u7a0b<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u6570\u636e\u8f93\u5165\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def input_data():<\/p>\n<p>    data = input(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff1a&quot;)<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=input_data)<\/p>\n<p>thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u53d1\u6570\u636e\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5e76\u884c\u6570\u636e\u8f93\u5165\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Process<\/p>\n<p>def input_data():<\/p>\n<p>    data = input(&quot;\u8bf7\u8f93\u5165\u6570\u636e\uff1a&quot;)<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u6570\u636e\uff1a&quot;, data)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b<\/strong><\/h2>\n<p>process = Process(target=input_data)<\/p>\n<p>process.start()<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b9e\u73b0\u5e76\u884c\u6570\u636e\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u4fe1\u606f\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528INI\u6587\u4ef6<\/li>\n<li>\u4f7f\u7528YAML\u6587\u4ef6<\/li>\n<\/ol>\n<p><h4>1.\u4f7f\u7528INI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>INI\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efaConfigParser\u5bf9\u8c61<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u8bfb\u53d6INI\u6587\u4ef6<\/strong><\/h2>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u914d\u7f6e\u4fe1\u606f<\/strong><\/h2>\n<p>database = config[&#39;DATABASE&#39;]<\/p>\n<p>host = database[&#39;host&#39;]<\/p>\n<p>port = database.getint(&#39;port&#39;)<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u4e3b\u673a\uff1a&quot;, host)<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u7aef\u53e3\uff1a&quot;, port)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceINI\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2.\u4f7f\u7528YAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>YAML\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u8bfb\u53d6YAML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<h2><strong>\u83b7\u53d6\u914d\u7f6e\u4fe1\u606f<\/strong><\/h2>\n<p>database = config[&#39;database&#39;]<\/p>\n<p>host = database[&#39;host&#39;]<\/p>\n<p>port = database[&#39;port&#39;]<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u4e3b\u673a\uff1a&quot;, host)<\/p>\n<p>print(&quot;\u6570\u636e\u5e93\u7aef\u53e3\uff1a&quot;, port)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4eceYAML\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8f93\u5165\u591a\u6570\u636e\uff0c\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41sys.stdin\u3001\u4f7f\u7528GUI\u5e93\u3001\u4f7f\u7528\u7f51\u7edc\u8f93\u5165\u3001\u4f7f\u7528\u6570\u636e\u5e93\u8f93\u5165\u3001\u4f7f\u7528\u6d88\u606f\u961f\u5217\u3001\u4f7f\u7528\u6d41\u5f0f\u6570\u636e\u5904\u7406\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7b49\u3002\u4e0d\u540c\u7684\u8f93\u5165\u65b9\u5f0f\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u636e\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u591a\u6761\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u591a\u6761\u6570\u636e\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u7ed3\u5408\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u591a\u6761\u6570\u636e\uff0c\u901a\u8fc7\u7279\u5b9a\u7684\u5206\u9694\u7b26\uff08\u5982\u9017\u53f7\uff09\u5206\u9694\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">data = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u636e\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;)\ndata_list = data.split(&#39;,&#39;)\nprint(data_list)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u8f93\u5165\u201c1,2,3\u201d\u5c06\u4f1a\u8f93\u51fa<code>[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5728\u63a5\u6536\u591a\u6761\u6570\u636e\u65f6\uff0c\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u8f93\u5165\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u7528\u6237\u8f93\u5165\u7684\u90fd\u662f\u6570\u5b57\uff0c\u53ef\u4ee5\u8fd9\u6837\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-python\">data = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;)\ntry:\n    data_list = [int(x) for x in data.split(&#39;,&#39;)]\n    print(data_list)\nexcept ValueError:\n    print(&quot;\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u5b57\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u53ea\u6709\u6709\u6548\u6570\u5b57\u88ab\u5904\u7406\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u591a\u6761\u8f93\u5165\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u7528\u6237\u8f93\u5165\u7684\u591a\u6761\u6570\u636e\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7ed3\u6784\u3002\u4f7f\u7528\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u5982\u4e0a\u6240\u793a\u3002\u800c\u5982\u679c\u9700\u8981\u5b58\u50a8\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u8f93\u5165\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u5b58\u50a8\u4e3a\u5b57\u5178\uff1a<\/p>\n<pre><code class=\"language-python\">data = input(&quot;\u8bf7\u8f93\u5165\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u7528\u9017\u53f7\u5206\u9694\uff08\u5982\uff1a\u5f20\u4e09,25\uff09\uff1a&quot;)\nname, age = data.split(&#39;,&#39;)\nuser_info = {name: int(age)}\nprint(user_info)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5f62\u5982<code>{&#39;\u5f20\u4e09&#39;: 25}<\/code>\u7684\u5b57\u5178\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u8f93\u5165\u591a\u6570\u636e\uff1a\u4f7f\u7528input()\u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41 [&hellip;]","protected":false},"author":3,"featured_media":1156386,"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\/1156378"}],"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=1156378"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156378\/revisions"}],"predecessor-version":[{"id":1156390,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156378\/revisions\/1156390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156386"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}