{"id":1073643,"date":"2025-01-08T11:27:32","date_gmt":"2025-01-08T03:27:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073643.html"},"modified":"2025-01-08T11:27:35","modified_gmt":"2025-01-08T03:27:35","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e8%be%93%e5%87%ba%e7%9a%84%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073643.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u5165\u8f93\u51fa\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103501\/4dc453c6-4c7a-417c-8122-8180b0451f28.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u5165\u8f93\u51fa\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u5165\u8f93\u51fa\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3001\u4f7f\u7528print()\u51fd\u6570\u6765\u8f93\u51fa\u7ed3\u679c\u3001\u4ee5\u53ca\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u8fdb\u884c\u8f93\u5165\u8f93\u51fa\u3002<\/strong> \u901a\u8fc7\u8fd9\u4e9b\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u7528\u6237\u7684\u4ea4\u4e92\u3001\u6570\u636e\u7684\u5b58\u50a8\u548c\u8bfb\u53d6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u4f7f\u7528<\/strong>\uff1a<\/p>\n<p><code>input()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u4e2d\u8bfb\u53d6\u4e00\u884c\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u63d0\u793a\u5b57\u7b26\u4e32\u6765\u5f15\u5bfc\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/strong>\uff1a<\/p>\n<p>\u7528\u6237\u8f93\u5165\u7684\u503c\u9ed8\u8ba4\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u82e5\u9700\u8981\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff09\uff0c\u9700\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>print(f&quot;Next year, you will be {age + 1} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38\u8f93\u5165<\/strong>\uff1a<\/p>\n<p>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u8f93\u5165\uff0c\u4f7f\u7528try-except\u5757\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u7528\u6237\u7684\u9519\u8bef\u8f93\u5165\u800c\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    number = int(input(&quot;Enter a number: &quot;))<\/p>\n<p>    print(f&quot;Twice the number is {number * 2}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;That&#39;s not a valid number!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u7ed3\u679c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u4f7f\u7528<\/strong>\uff1a<\/p>\n<p><code>print()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9a\u5185\u5bb9\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\uff08\u5c4f\u5e55\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff1a<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5bf9\u8f93\u51fa\u5185\u5bb9\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u5305\u62ec\u4f7f\u7528f-string\u3001<code>format()<\/code>\u65b9\u6cd5\u548c\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Using f-string (Python 3.6+)<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<h2><strong>Using format() method<\/strong><\/h2>\n<p>print(&quot;Hello, {}!&quot;.format(name))<\/p>\n<h2><strong>Using % formatting<\/strong><\/h2>\n<p>print(&quot;Hello, %s!&quot; % name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63a7\u5236\u8f93\u51fa\u65b9\u5f0f<\/strong>\uff1a<\/p>\n<p><code>print()<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6765\u63a7\u5236\u8f93\u51fa\u65b9\u5f0f\uff0c\u5982end\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u7ed3\u675f\u65f6\u7684\u5b57\u7b26\uff0csep\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, &quot;World&quot;, sep=&quot;, &quot;, end=&quot;!\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u6587\u4ef6\u64cd\u4f5c\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u6307\u5b9a\u6587\u4ef6\u540d\u53ca\u64cd\u4f5c\u6a21\u5f0f\uff08\u8bfb\u53d6&#39;r&#39;\u3001\u5199\u5165&#39;w&#39;\u3001\u8ffd\u52a0&#39;a&#39;\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;r&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong>\uff1a<\/p>\n<p>\u6587\u4ef6\u5bf9\u8c61\u63d0\u4f9b\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5982<code>read()<\/code>\u3001<code>readline()<\/code>\u3001<code>readlines()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content = file.read()<\/p>\n<p>print(content)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5199\u5165\u6587\u4ef6<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\uff0c\u5199\u5165\u64cd\u4f5c\u5b8c\u6210\u540e\u8981\u8bb0\u5f97\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p>file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 with \u8bed\u53e5<\/strong>\uff1a<\/p>\n<p>with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>sys<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ece\u547d\u4ee4\u884c\u8bfb\u53d6\u8f93\u5165<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528<code>sys.stdin<\/code>\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>for line in sys.stdin:<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa<\/strong>\uff1a<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u5206\u522b\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u5199\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.stdout.write(&quot;This is standard output\\n&quot;)<\/p>\n<p>sys.stderr.write(&quot;This is standard error\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u4f7f\u7528<\/strong>\uff1a<\/p>\n<p><code>argparse<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4fbf\u4e8e\u7f16\u5199\u7528\u6237\u53cb\u597d\u7684\u547d\u4ee4\u884c\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;A simple argument parser example&quot;)<\/p>\n<p>parser.add_argument(&quot;name&quot;, type=str, help=&quot;Your name&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(f&quot;Hello, {args.name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u53ef\u9009\u53c2\u6570<\/strong>\uff1a<\/p>\n<p>\u53ef\u4ee5\u6dfb\u52a0\u53ef\u9009\u53c2\u6570\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser.add_argument(&quot;-a&quot;, &quot;--age&quot;, type=int, help=&quot;Your age&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>if args.age:<\/p>\n<p>    print(f&quot;Hello, {args.name}! You are {args.age} years old.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Hello, {args.name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u548c\u5199\u5165 JSON \u6570\u636e<\/strong>\uff1a<\/p>\n<p><code>json<\/code>\u6a21\u5757\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u89e3\u6790\u548c\u751f\u6210 JSON \u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<h2><strong>Writing JSON data to a file<\/strong><\/h2>\n<p>with open(&quot;data.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<h2><strong>Reading JSON data from a file<\/strong><\/h2>\n<p>with open(&quot;data.json&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u548c\u5199\u5165 CSV \u6570\u636e<\/strong>\uff1a<\/p>\n<p><code>csv<\/code>\u6a21\u5757\u7528\u4e8e\u5904\u7406 CSV \u6587\u4ef6\uff0c\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u63a5\u53e3\u6765\u8bfb\u53d6\u548c\u5199\u5165 CSV \u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>Writing CSV data to a file<\/strong><\/h2>\n<p>with open(&quot;data.csv&quot;, &quot;w&quot;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;])<\/p>\n<p>    writer.writerow([&quot;Alice&quot;, 25, &quot;New York&quot;])<\/p>\n<p>    writer.writerow([&quot;Bob&quot;, 30, &quot;Los Angeles&quot;])<\/p>\n<h2><strong>Reading CSV data from a file<\/strong><\/h2>\n<p>with open(&quot;data.csv&quot;, &quot;r&quot;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u4f7f\u7528 <code>pickle<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u6570\u636e<\/strong>\uff1a<\/p>\n<p><code>pickle<\/code>\u6a21\u5757\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316 Python \u5bf9\u8c61\uff0c\u4fbf\u4e8e\u5c06\u590d\u6742\u6570\u636e\u7ed3\u6784\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<h2><strong>Serializing data to a file<\/strong><\/h2>\n<p>with open(&quot;data.pkl&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    pickle.dump(data, file)<\/p>\n<h2><strong>Deserializing data from a file<\/strong><\/h2>\n<p>with open(&quot;data.pkl&quot;, &quot;rb&quot;) as file:<\/p>\n<p>    data = pickle.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u4f7f\u7528 <code>sqlite3<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u64cd\u4f5c SQLite \u6570\u636e\u5e93<\/strong>\uff1a<\/p>\n<p><code>sqlite3<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e SQLite \u6570\u636e\u5e93\u4ea4\u4e92\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>Connecting to a database (or creating it)<\/strong><\/h2>\n<p>conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>Creating a table<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                  (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>Inserting data into the table<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 25)&quot;)<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>Querying data from the table<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>Closing the connection<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u3001\u4f7f\u7528 <code>requests<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u53d1\u9001 HTTP \u8bf7\u6c42<\/strong>\uff1a<\/p>\n<p><code>requests<\/code>\u6a21\u5757\u7528\u4e8e\u53d1\u9001 HTTP \u8bf7\u6c42\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e Web \u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>Sending a GET request<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/api.github.com&quot;)<\/p>\n<p>print(response.json())<\/p>\n<h2><strong>Sending a POST request<\/strong><\/h2>\n<p>data = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>response = requests.post(&quot;https:\/\/httpbin.org\/post&quot;, json=data)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528 <code>subprocess<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6267\u884c\u7cfb\u7edf\u547d\u4ee4<\/strong>\uff1a<\/p>\n<p><code>subprocess<\/code>\u6a21\u5757\u7528\u4e8e\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u53ef\u4ee5\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>Executing a system command<\/strong><\/h2>\n<p>result = subprocess.run([&quot;echo&quot;, &quot;Hello, World!&quot;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528 <code>io<\/code> \u6a21\u5757\u8fdb\u884c\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u64cd\u4f5c\u5185\u5b58\u4e2d\u7684\u6587\u4ef6<\/strong>\uff1a<\/p>\n<p><code>io<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5728\u5185\u5b58\u4e2d\u64cd\u4f5c\u6587\u4ef6\u7684\u63a5\u53e3\uff0c\u5982<code>StringIO<\/code>\u548c<code>BytesIO<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from io import StringIO<\/p>\n<h2><strong>Creating an in-memory text stream<\/strong><\/h2>\n<p>text_stream = StringIO()<\/p>\n<p>text_stream.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p>text_stream.write(&quot;This is a test.\\n&quot;)<\/p>\n<h2><strong>Reading from the in-memory text stream<\/strong><\/h2>\n<p>text_stream.seek(0)<\/p>\n<p>print(text_stream.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5404\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u8fdb\u884c\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4e0e\u7528\u6237\u4ea4\u4e92\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u8fd8\u662f\u6570\u636e\u5e93\u64cd\u4f5c\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u6a21\u5757\u6765\u5b9e\u73b0\u9ad8\u6548\u7684\u8f93\u5165\u8f93\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u8be5\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)\nprint(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)\n<\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u51fa\u7ed3\u679c\u683c\u5f0f\u5316\u4e3a\u66f4\u6613\u8bfb\u7684\u5f62\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63d0\u9ad8\u8f93\u51fa\u7ed3\u679c\u7684\u53ef\u8bfb\u6027\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528f-string\u3001<code>format()<\/code>\u65b9\u6cd5\u548c\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528f-string\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nprint(f&quot;{name}\u7684\u5e74\u9f84\u662f{age}\u5c81\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u8f93\u51fa\u5185\u5bb9\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u8981\u5c06\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u5199\u5165\u6a21\u5f0f\uff08&#39;w&#39;\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;w&quot;) as file:\n    file.write(&quot;\u8fd9\u662f\u8981\u4fdd\u5b58\u7684\u5185\u5bb9\u3002&quot;)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u5165\u8f93\u51fa\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3001\u4f7f\u7528print()\u51fd\u6570\u6765\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":1073656,"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\/1073643"}],"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=1073643"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073643\/revisions"}],"predecessor-version":[{"id":1073657,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073643\/revisions\/1073657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073656"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}