{"id":1152840,"date":"2025-01-13T17:30:49","date_gmt":"2025-01-13T09:30:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152840.html"},"modified":"2025-01-13T17:30:52","modified_gmt":"2025-01-13T09:30:52","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8a%a0%e5%85%a5%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152840.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u52a0\u5165\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182725\/9f369cff-4cd2-4693-9d1c-d0a4eedca2bb.webp\" alt=\"python\u4e2d\u5982\u4f55\u52a0\u5165\u8f93\u5165\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<\/strong>\u3001<strong><code>input()<\/code>\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u8fd4\u56de\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong><code>input()<\/code>\u51fd\u6570<\/strong> \u662f\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5728\u63d0\u793a\u6587\u672c\u4e2d\u5411\u7528\u6237\u8bf4\u660e\u9700\u8981\u8f93\u5165\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a<code>name = input(&quot;Enter your name: &quot;)<\/code>\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5e76\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u53d8\u91cf <code>name<\/code>\u3002\u5728\u9700\u8981\u6574\u6570\u6216\u6d6e\u70b9\u6570\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u6570\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<code>age = int(input(&quot;Enter your age: &quot;))<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5982\u4f55\u52a0\u5165\u8f93\u5165\uff0c\u5305\u62ec\u4e0d\u540c\u573a\u666f\u4e0b\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u57fa\u672c\u8f93\u5165<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u57fa\u672c\u7684\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u8fd4\u56de\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>print(&quot;Hello, &quot; + name + &quot;!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u540d\u5b57\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u4e0e\u5b57\u7b26\u4e32 &quot;Hello, &quot; \u8fde\u63a5\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6570\u503c\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>input()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u6570\u503c\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u6216 <code>float()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>print(&quot;You are &quot; + str(age) + &quot; years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u5904\u7406\u5e03\u5c14\u503c\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e03\u5c14\u503c\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">answer = input(&quot;Do you like Python? (yes\/no): &quot;).lower()<\/p>\n<p>if answer == &quot;yes&quot;:<\/p>\n<p>    likes_python = True<\/p>\n<p>else:<\/p>\n<p>    likes_python = False<\/p>\n<p>print(&quot;Likes Python:&quot;, likes_python)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165 &quot;yes&quot; \u6216 &quot;no&quot;\uff0c\u7136\u540e\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u9ad8\u7ea7\u8f93\u5165\u5904\u7406<\/strong><\/h2>\n<p><h2>1\u3001\u5904\u7406\u591a\u884c\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u884c\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u67d0\u4e2a\u7279\u6b8a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u8868\u793a\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Enter text (type &#39;END&#39; to finish):&quot;)<\/p>\n<p>lines = []<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;END&quot;:<\/p>\n<p>        break<\/p>\n<p>    lines.append(line)<\/p>\n<p>text = &quot;\\n&quot;.join(lines)<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6bcf\u4e00\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 &quot;END&quot; \u4e3a\u6b62\uff0c\u7136\u540e\u5c06\u6240\u6709\u884c\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u8f93\u5165\u4e2d\u7684\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u7528\u6237\u8f93\u5165\u4e86\u975e\u6570\u503c\u5b57\u7b26\u5bfc\u81f4\u8f6c\u6362\u5931\u8d25\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code> \u548c <code>except<\/code> \u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>        break<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;Invalid input. Please enter a number.&quot;)<\/p>\n<p>print(&quot;You are &quot; + str(age) + &quot; years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u5e74\u9f84\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u975e\u6570\u503c\u5b57\u7b26\uff0c\u4f1a\u6355\u83b7 <code>ValueError<\/code> \u5f02\u5e38\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_pattern = r&quot;^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$&quot;<\/p>\n<p>while True:<\/p>\n<p>    email = input(&quot;Enter your email: &quot;)<\/p>\n<p>    if re.match(email_pattern, email):<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Invalid email format. Please try again.&quot;)<\/p>\n<p>print(&quot;Your email is &quot; + email)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u8f93\u5165\u7684\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u56fe\u5f62\u754c\u9762\u8f93\u5165<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528 Tkinter \u83b7\u53d6\u8f93\u5165<\/h2>\n<\/p>\n<p><p>Tkinter \u662f Python \u7684\u6807\u51c6\u56fe\u5f62\u754c\u9762\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u56fe\u5f62\u754c\u9762\u7684\u8f93\u5165\u8868\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>name = simpledialog.askstring(&quot;Input&quot;, &quot;Enter your name:&quot;)<\/p>\n<p>print(&quot;Hello, &quot; + name + &quot;!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5f39\u51fa\u4e00\u4e2a\u8f93\u5165\u5bf9\u8bdd\u6846\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u540d\u5b57\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u8f93\u5165\u8868\u5355<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 Tkinter \u521b\u5efa\u66f4\u590d\u6742\u7684\u81ea\u5b9a\u4e49\u8f93\u5165\u8868\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def submit():<\/p>\n<p>    name = entry_name.get()<\/p>\n<p>    age = entry_age.get()<\/p>\n<p>    print(&quot;Name:&quot;, name)<\/p>\n<p>    print(&quot;Age:&quot;, age)<\/p>\n<p>    root.destroy()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Input Form&quot;)<\/p>\n<p>tk.Label(root, text=&quot;Name:&quot;).grid(row=0)<\/p>\n<p>tk.Label(root, text=&quot;Age:&quot;).grid(row=1)<\/p>\n<p>entry_name = tk.Entry(root)<\/p>\n<p>entry_age = tk.Entry(root)<\/p>\n<p>entry_name.grid(row=0, column=1)<\/p>\n<p>entry_age.grid(row=1, column=1)<\/p>\n<p>tk.Button(root, text=&quot;Submit&quot;, command=submit).grid(row=2, column=1)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u8f93\u5165\u5b57\u6bb5\u548c\u4e00\u4e2a\u63d0\u4ea4\u6309\u94ae\u7684\u8868\u5355\u3002\u7528\u6237\u8f93\u5165\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u7136\u540e\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u8f93\u5165\u7684\u5185\u5bb9\u5e76\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u547d\u4ee4\u884c\u53c2\u6570\u8f93\u5165<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528 <code>sys.argv<\/code> \u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u83b7\u53d6\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528 <code>sys.argv<\/code> \u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) != 3:<\/p>\n<p>    print(&quot;Usage: python script.py &lt;name&gt; &lt;age&gt;&quot;)<\/p>\n<p>    sys.exit(1)<\/p>\n<p>name = sys.argv[1]<\/p>\n<p>age = int(sys.argv[2])<\/p>\n<p>print(&quot;Hello, &quot; + name + &quot;! You are &quot; + str(age) + &quot; years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528 argparse \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>argparse<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u76f8\u6bd4 <code>sys.argv<\/code>\uff0c<code>argparse<\/code> \u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u53cb\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;Process some integers.&quot;)<\/p>\n<p>parser.add_argument(&quot;name&quot;, type=str, help=&quot;Your name&quot;)<\/p>\n<p>parser.add_argument(&quot;age&quot;, type=int, help=&quot;Your age&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(&quot;Hello, &quot; + args.name + &quot;! You are &quot; + str(args.age) + &quot; years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u5c06\u89e3\u6790\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728 <code>args<\/code> \u5bf9\u8c61\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u6587\u4ef6\u8f93\u5165<\/strong><\/h2>\n<p><h2>1\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 <code>read()<\/code> \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>print(&quot;File content:&quot;)<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5f00\u540d\u4e3a &quot;input.txt&quot; \u7684\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 <code>readlines()<\/code> \u65b9\u6cd5\u6216\u8005\u76f4\u63a5\u5728\u5faa\u73af\u4e2d\u904d\u5386\u6587\u4ef6\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u7f51\u7edc\u8f93\u5165<\/strong><\/h2>\n<p><h2>1\u3001\u4ece\u7f51\u9875\u83b7\u53d6\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>requests<\/code> \u5e93\u4ece\u7f51\u9875\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>content = response.text<\/p>\n<p>print(&quot;Web page content:&quot;)<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4ece\u6307\u5b9a\u7684 URL \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406 JSON \u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8f93\u5165\u65f6\uff0c\u901a\u5e38\u4f1a\u9047\u5230 JSON \u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528 <code>requests<\/code> \u5e93\u7684 <code>json()<\/code> \u65b9\u6cd5\u5c06\u54cd\u5e94\u5185\u5bb9\u89e3\u6790\u4e3a JSON \u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/api.example.com\/data&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>data = response.json()<\/p>\n<p>print(&quot;JSON data:&quot;)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4ece\u6307\u5b9a\u7684 API \u83b7\u53d6 JSON \u6570\u636e\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a Python \u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u7efc\u5408\u5e94\u7528<\/strong><\/h2>\n<p><h2>1\u3001\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f<\/h2>\n<\/p>\n<p><p>\u7ed3\u5408\u524d\u9762\u4ecb\u7ecd\u7684\u5404\u79cd\u8f93\u5165\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7528\u6237\u6ce8\u518c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<p>import re<\/p>\n<p>def validate_email(email):<\/p>\n<p>    email_pattern = r&quot;^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$&quot;<\/p>\n<p>    return re.match(email_pattern, email)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.withdraw()<\/p>\n<p>name = simpledialog.askstring(&quot;Input&quot;, &quot;Enter your name:&quot;)<\/p>\n<p>age = simpledialog.askinteger(&quot;Input&quot;, &quot;Enter your age:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    email = simpledialog.askstring(&quot;Input&quot;, &quot;Enter your email:&quot;)<\/p>\n<p>    if validate_email(email):<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Invalid email format. Please try again.&quot;)<\/p>\n<p>print(&quot;User Information:&quot;)<\/p>\n<p>print(&quot;Name:&quot;, name)<\/p>\n<p>print(&quot;Age:&quot;, age)<\/p>\n<p>print(&quot;Email:&quot;, email)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u4f9d\u6b21\u8f93\u5165\u540d\u5b57\u3001\u5e74\u9f84\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5e76\u5bf9\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u8fdb\u884c\u683c\u5f0f\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u548c\u6587\u4ef6\u8f93\u5165\u7684\u7efc\u5408\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408\u547d\u4ee4\u884c\u53c2\u6570\u548c\u6587\u4ef6\u8f93\u5165\uff0c\u6784\u5efa\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u5206\u6790\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;Analyze log file.&quot;)<\/p>\n<p>parser.add_argument(&quot;logfile&quot;, type=str, help=&quot;Path to the log file&quot;)<\/p>\n<p>parser.add_argument(&quot;keyword&quot;, type=str, help=&quot;Keyword to search for&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>with open(args.logfile, &quot;r&quot;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>count = 0<\/p>\n<p>for line in lines:<\/p>\n<p>    if args.keyword in line:<\/p>\n<p>        print(line.strip())<\/p>\n<p>        count += 1<\/p>\n<p>print(f&quot;Found {count} occurrences of &#39;{args.keyword}&#39; in {args.logfile}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\u548c\u641c\u7d22\u5173\u952e\u5b57\uff0c\u7136\u540e\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5e76\u641c\u7d22\u5173\u952e\u5b57\uff0c\u6700\u540e\u6253\u5370\u51fa\u5305\u542b\u5173\u952e\u5b57\u7684\u884c\u548c\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\u548c\u793a\u4f8b\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u8f93\u5165\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u7528\u6237\u8f93\u5165\u3001\u6587\u4ef6\u8f93\u5165\u3001\u7f51\u7edc\u8f93\u5165\uff0c\u8fd8\u662f\u547d\u4ee4\u884c\u53c2\u6570\u8f93\u5165\uff0c\u90fd\u6709\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u548c\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u8be5\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u952e\u5165\u4fe1\u606f\u5e76\u6309\u56de\u8f66\u952e\u3002\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u4f8b\u5982\uff0c<code>user_input = input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u540d\u5b57: &quot;)<\/code>\u5c06\u63d0\u793a\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\uff0c\u5e76\u5c06\u8f93\u5165\u4fdd\u5b58\u5230<code>user_input<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u7c7b\u578b\u7684\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u867d\u7136<code>input()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\u5c06\u5176\u8f6c\u53d8\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u83b7\u53d6\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\uff1a<code>age = int(input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u5e74\u9f84: &quot;))<\/code>\u3002\u7c7b\u4f3c\u5730\uff0c\u6d6e\u70b9\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>float()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u6216\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6548\u6570\u636e\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u65f6\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u7a0b\u5e8f\u5c06\u629b\u51fa\u5f02\u5e38\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    age = int(input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u5e74\u9f84: &quot;))\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u53cd\u9988\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684input()\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u3001input()\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1152844,"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\/1152840"}],"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=1152840"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152840\/revisions"}],"predecessor-version":[{"id":1152846,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152840\/revisions\/1152846"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152844"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}