{"id":1066593,"date":"2024-12-31T16:27:50","date_gmt":"2024-12-31T08:27:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066593.html"},"modified":"2024-12-31T16:27:52","modified_gmt":"2024-12-31T08:27:52","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e4%bb%bb%e6%84%8f%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066593.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u4efb\u610f\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e7c5237f-7339-4665-894f-0ecfb9929cc2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u4efb\u610f\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8f93\u5165\u4efb\u610f\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528input\u51fd\u6570\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u3001\u4ece\u7f51\u7edc\u8bfb\u53d6\u7b49\u3002<\/strong> \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7input\u51fd\u6570\u8ba9\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528input\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528input\u51fd\u6570\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u8fd0\u884c\u7a0b\u5e8f\u65f6\u8f93\u5165\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u8f93\u5165\uff0c\u5c24\u5176\u662f\u5728\u4ea4\u4e92\u5f0f\u811a\u672c\u4e2d\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u636e<\/li>\n<li>\u8bfb\u53d6\u8f93\u5165\u7684\u6570\u636e<\/li>\n<li>\u5c06\u8f93\u5165\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u636e<\/p>\n<p>input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff08\u4f7f\u7528\u9017\u53f7\u5206\u9694\u5143\u7d20\uff09\uff1a&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u8f93\u5165\u7684\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>input_list = input_str.split(&#39;,&#39;)<\/p>\n<h2><strong>\u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\uff08\u5982\u679c\u6709\uff09<\/strong><\/h2>\n<p>input_list = [element.strip() for element in input_list]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u8bfb\u53d6\u8f93\u5165\u7684\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>split(&#39;,&#39;)<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u5206\u9694\uff0c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8f93\u5165\u7684\u6570\u636e\u91cf\u8f83\u5927\uff0c\u624b\u52a8\u8f93\u5165\u663e\u7136\u4e0d\u73b0\u5b9e\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u4e2d\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u6570\u636e\u5b58\u50a8\u5728\u6587\u672c\u6587\u4ef6\u4e2d<\/li>\n<li>\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/li>\n<li>\u5c06\u6587\u4ef6\u5185\u5bb9\u8f6c\u6362\u4e3a\u5217\u8868<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>data.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>apple, banana, cherry, date<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6587\u4ef6\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    file_content = file.read()<\/p>\n<h2><strong>\u5c06\u6587\u4ef6\u5185\u5bb9\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>input_list = file_content.split(&#39;,&#39;)<\/p>\n<h2><strong>\u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\uff08\u5982\u679c\u6709\uff09<\/strong><\/h2>\n<p>input_list = [element.strip() for element in input_list]<\/p>\n<p>print(&quot;\u8bfb\u53d6\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>with open(&#39;data.txt&#39;, &#39;r&#39;) as file<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>split(&#39;,&#39;)<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u6309\u9017\u53f7\u5206\u9694\uff0c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4ece\u7f51\u7edc\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u7f51\u7edc\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6e90\uff0c\u5982API\u3001\u7f51\u9875\u7b49\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u6570\u636e<\/li>\n<li>\u89e3\u6790\u54cd\u5e94\u6570\u636e<\/li>\n<li>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u4e00\u4e2aAPI\u83b7\u53d6\u6570\u636e\uff0c\u8be5API\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>&quot;apple, banana, cherry, date&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53d1\u9001\u8bf7\u6c42\u5e76\u89e3\u6790\u54cd\u5e94\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com\/api\/data&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    # \u83b7\u53d6\u54cd\u5e94\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<p>    input_list = response.text.split(&#39;,&#39;)<\/p>\n<p>    # \u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\uff08\u5982\u679c\u6709\uff09<\/p>\n<p>    input_list = [element.strip() for element in input_list]<\/p>\n<p>    print(&quot;\u83b7\u53d6\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a&quot;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get(&#39;https:\/\/example.com\/api\/data&#39;)<\/code>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f\uff08\u72b6\u6001\u7801\u4e3a200\uff09\u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u6211\u4eec\u4f7f\u7528<code>response.text.split(&#39;,&#39;)<\/code>\u65b9\u6cd5\u5c06\u54cd\u5e94\u6570\u636e\u6309\u9017\u53f7\u5206\u9694\uff0c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4ece\u5176\u4ed6\u6570\u636e\u6e90\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u3001Excel\u6587\u4ef6\u3001JSON\u6587\u4ef6\u7b49\u5176\u4ed6\u6570\u636e\u6e90\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5177\u4f53\u6b65\u9aa4\u53d6\u51b3\u4e8e\u6570\u636e\u6e90\u7684\u7c7b\u578b\u548c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4ece\u6570\u636e\u5e93\u8bfb\u53d6<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4eceSQLite\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\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\u5e76\u83b7\u53d6\u7ed3\u679c<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT column_name FROM table_name&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>input_list = [row[0] for row in rows]<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p>print(&quot;\u8bfb\u53d6\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\u5e76\u521b\u5efa\u6e38\u6807\uff0c\u7136\u540e\u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6700\u540e\uff0c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4eceExcel\u6587\u4ef6\u8bfb\u53d6<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4eceExcel\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;example.xlsx&#39;)<\/p>\n<h2><strong>\u5c06\u6307\u5b9a\u5217\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>input_list = df[&#39;column_name&#39;].tolist()<\/p>\n<p>print(&quot;\u8bfb\u53d6\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas.read_excel(&#39;example.xlsx&#39;)<\/code>\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u6307\u5b9a\u5217\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4eceJSON\u6587\u4ef6\u8bfb\u53d6<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4eceJSON\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6253\u5f00JSON\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u5c06\u6307\u5b9a\u952e\u7684\u503c\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>input_list = data[&#39;key_name&#39;]<\/p>\n<p>print(&quot;\u8bfb\u53d6\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>with open(&#39;example.json&#39;, &#39;r&#39;) as file<\/code>\u8bed\u53e5\u6253\u5f00JSON\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u6307\u5b9a\u952e\u7684\u503c\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e2d\u8f93\u5165\u4efb\u610f\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u6570\u636e\u6765\u6e90\u548c\u4f7f\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u7684\u4ea4\u4e92\u5f0f\u6570\u636e\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u6587\u4ef6\u3001\u7f51\u7edc\u3001\u6570\u636e\u5e93\u3001Excel\u3001JSON\u7b49\u6570\u636e\u6e90\u8bfb\u53d6\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u662f\u7406\u89e3\u6570\u636e\u7684\u683c\u5f0f\u548c\u7ed3\u6784\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u5de5\u5177\u548c\u6280\u672f\u8fdb\u884c\u8f6c\u6362\u548c\u5904\u7406\u3002\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u8f93\u5165\u4efb\u610f\u5217\u8868\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u5e76\u786e\u4fdd\u5176\u5143\u7d20\u7684\u7c7b\u578b\u4e00\u81f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u7ed3\u5408<code>eval()<\/code>\u6216<code>split()<\/code>\u65b9\u6cd5\u6765\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u3002\u5982\u679c\u5e0c\u671b\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e00\u81f4\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff08\u7528\u9017\u53f7\u5206\u9694\uff09: &quot;).split(&#39;,&#39;)<\/code>\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u5217\u8868\uff0c\u518d\u901a\u8fc7<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u4ee5\u786e\u4fdd\u5217\u8868\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u8f93\u5165\u9519\u8bef\u5e76\u7ed9\u51fa\u63d0\u793a\u3002\u5f53\u7528\u6237\u8f93\u5165\u4e0d\u7b26\u5408\u9884\u671f\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u540e\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\u548c\u5143\u7d20\u7c7b\u578b\uff0c\u82e5\u4e0d\u7b26\u5408\u8981\u6c42\u5219\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u518d\u6b21\u8bf7\u6c42\u8f93\u5165\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5217\u8868\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u968f\u673a\u6570\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u3002\u901a\u8fc7<code>random.sample()<\/code>\u6216<code>random.randint()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u624b\u52a8\u8f93\u5165\u66f4\u5feb\u901f\u4e14\u6709\u6548\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8f93\u5165\u4efb\u610f\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528input\u51fd\u6570\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u3001\u4ece\u7f51\u7edc\u8bfb\u53d6\u7b49\u3002 \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6700\u5e38\u7528\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1066602,"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\/1066593"}],"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=1066593"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066593\/revisions"}],"predecessor-version":[{"id":1066603,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066593\/revisions\/1066603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066602"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}