{"id":1026299,"date":"2024-12-31T10:41:22","date_gmt":"2024-12-31T02:41:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026299.html"},"modified":"2024-12-31T10:41:24","modified_gmt":"2024-12-31T02:41:24","slug":"%e6%91%84%e6%b0%8f%e5%ba%a6%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1026299.html","title":{"rendered":"\u6444\u6c0f\u5ea6\u5728python\u4e2d\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/27839e10-57c3-4109-9fd4-9edaa522bf2d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u6444\u6c0f\u5ea6\u5728python\u4e2d\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6444\u6c0f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\u548c\u5904\u7406\u3002<strong>\u4f7f\u7528\u9002\u5f53\u7684\u53d8\u91cf\u540d\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5229\u7528\u6e29\u5ea6\u8f6c\u6362\u51fd\u6570<\/strong>\u7b49\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\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\u9002\u5f53\u7684\u53d8\u91cf\u540d<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u540d\u53ef\u4ee5\u5e2e\u52a9\u6e05\u6670\u5730\u8868\u793a\u6e29\u5ea6\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">temperature_celsius = 25.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>temperature_celsius<\/code> \u53d8\u91cf\u7528\u6765\u5b58\u50a8\u6444\u6c0f\u6e29\u5ea6\u503c 25.0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u6216\u8005\u663e\u793a\u6e29\u5ea6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u8868\u793a\u6444\u6c0f\u5ea6\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>f-string<\/code>\uff0c<code>format<\/code> \u65b9\u6cd5\uff0c\u6216\u8005 <code>%<\/code> \u64cd\u4f5c\u7b26\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 f-string<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">temperature_celsius = 25.0<\/p>\n<p>print(f&quot;The temperature is {temperature_celsius}\u00b0C&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528 format \u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">temperature_celsius = 25.0<\/p>\n<p>print(&quot;The temperature is {}\u00b0C&quot;.format(temperature_celsius))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528 % \u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">temperature_celsius = 25.0<\/p>\n<p>print(&quot;The temperature is %.1f\u00b0C&quot; % temperature_celsius)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u6e29\u5ea6\u8f6c\u6362\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6e29\u5ea6\u8f6c\u6362\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002Python \u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u6444\u6c0f\u5ea6\u548c\u5176\u4ed6\u6e29\u5ea6\u5355\u4f4d\uff08\u5982\u534e\u6c0f\u5ea6\u3001\u5f00\u5c14\u6587\u7b49\uff09\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>\u6444\u6c0f\u5ea6\u8f6c\u6362\u4e3a\u534e\u6c0f\u5ea6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def celsius_to_fahrenheit(celsius):<\/p>\n<p>    return (celsius * 9\/5) + 32<\/p>\n<p>temperature_celsius = 25.0<\/p>\n<p>temperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius)<\/p>\n<p>print(f&quot;{temperature_celsius}\u00b0C is equal to {temperature_fahrenheit}\u00b0F&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6444\u6c0f\u5ea6\u8f6c\u6362\u4e3a\u5f00\u5c14\u6587<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def celsius_to_kelvin(celsius):<\/p>\n<p>    return celsius + 273.15<\/p>\n<p>temperature_celsius = 25.0<\/p>\n<p>temperature_kelvin = celsius_to_kelvin(temperature_celsius)<\/p>\n<p>print(f&quot;{temperature_celsius}\u00b0C is equal to {temperature_kelvin}K&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6e29\u5ea6\u6570\u636e\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5904\u7406\u6e29\u5ea6\u6570\u636e\u53ef\u80fd\u6d89\u53ca\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6570\u636e\u3001\u5b58\u50a8\u5230\u6570\u636e\u5e93\u3001\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6e29\u5ea6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6e29\u5ea6\u4f20\u611f\u5668\u8fde\u63a5\u5230\u4f60\u7684\u7cfb\u7edf\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u9002\u5f53\u7684\u5e93\uff08\u4f8b\u5982 <code>smbus<\/code> \u7528\u4e8e I2C \u8bbe\u5907\uff09\u8bfb\u53d6\u6e29\u5ea6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smbus<\/p>\n<p>def read_temperature_sensor(address):<\/p>\n<p>    bus = smbus.SMBus(1)<\/p>\n<p>    raw_data = bus.read_word_data(address, 0)<\/p>\n<p>    # \u5047\u8bbe\u4f20\u611f\u5668\u8fd4\u56de\u7684\u6570\u636e\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u8f6c\u6362<\/p>\n<p>    temperature_celsius = (raw_data &amp; 0xFF) * 256 + (raw_data &gt;&gt; 8 &amp; 0xFF)<\/p>\n<p>    temperature_celsius = temperature_celsius \/ 100.0<\/p>\n<p>    return temperature_celsius<\/p>\n<p>sensor_address = 0x48<\/p>\n<p>temperature_celsius = read_temperature_sensor(sensor_address)<\/p>\n<p>print(f&quot;Sensor temperature: {temperature_celsius}\u00b0C&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b58\u50a8\u6e29\u5ea6\u6570\u636e\u5230\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\uff08\u4f8b\u5982 SQLite\uff09\u6765\u5b58\u50a8\u6e29\u5ea6\u6570\u636e\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def create_database():<\/p>\n<p>    conn = sqlite3.connect(&#39;temperature_data.db&#39;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS temperatures<\/p>\n<p>                 (timestamp TEXT, temperature REAL)&#39;&#39;&#39;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def insert_temperature(temperature):<\/p>\n<p>    conn = sqlite3.connect(&#39;temperature_data.db&#39;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&quot;INSERT INTO temperatures (timestamp, temperature) VALUES (datetime(&#39;now&#39;), ?)&quot;, (temperature,))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>temperature_celsius = 25.0<\/p>\n<p>create_database()<\/p>\n<p>insert_temperature(temperature_celsius)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5206\u6790\u548c\u53ef\u89c6\u5316\u6e29\u5ea6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5206\u6790\u5e93\uff08\u4f8b\u5982 pandas\uff09\u548c\u53ef\u89c6\u5316\u5e93\uff08\u4f8b\u5982 matplotlib\uff09\u6765\u5206\u6790\u548c\u53ef\u89c6\u5316\u6e29\u5ea6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>def fetch_temperature_data():<\/p>\n<p>    conn = sqlite3.connect(&#39;temperature_data.db&#39;)<\/p>\n<p>    df = pd.read_sql_query(&quot;SELECT * FROM temperatures&quot;, conn)<\/p>\n<p>    conn.close()<\/p>\n<p>    return df<\/p>\n<p>df = fetch_temperature_data()<\/p>\n<p>df[&#39;timestamp&#39;] = pd.to_datetime(df[&#39;timestamp&#39;])<\/p>\n<p>plt.plot(df[&#39;timestamp&#39;], df[&#39;temperature&#39;])<\/p>\n<p>plt.xlabel(&#39;Timestamp&#39;)<\/p>\n<p>plt.ylabel(&#39;Temperature (\u00b0C)&#39;)<\/p>\n<p>plt.title(&#39;Temperature Over Time&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6444\u6c0f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\u548c\u5904\u7406\u3002<strong>\u4f7f\u7528\u9002\u5f53\u7684\u53d8\u91cf\u540d\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5229\u7528\u6e29\u5ea6\u8f6c\u6362\u51fd\u6570<\/strong>\u7b49\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6570\u636e\u3001\u5b58\u50a8\u5230\u6570\u636e\u5e93\u3001\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5904\u7406\u548c\u8868\u793a\u6444\u6c0f\u5ea6\u6e29\u5ea6\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6444\u6c0f\u5ea6\u8f6c\u6362\u4e3a\u534e\u6c0f\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6570\u5b66\u516c\u5f0f\u5c06\u6444\u6c0f\u5ea6\u8f6c\u6362\u4e3a\u534e\u6c0f\u5ea6\u3002\u516c\u5f0f\u4e3a\uff1a\u534e\u6c0f\u5ea6 = (\u6444\u6c0f\u5ea6 \u00d7 9\/5) + 32\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">celsius = 25  # \u8f93\u5165\u6444\u6c0f\u5ea6\nfahrenheit = (celsius * 9\/5) + 32\nprint(f&quot;\u6444\u6c0f\u5ea6 {celsius} \u8f6c\u6362\u4e3a\u534e\u6c0f\u5ea6\u662f {fahrenheit}&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5904\u7406\u6e29\u5ea6\u8f6c\u6362\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u6e29\u5ea6\u8f6c\u6362\uff0c\u4f8b\u5982NumPy\u548cPandas\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6279\u91cf\u6570\u636e\u7684\u6e29\u5ea6\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u8fdb\u884c\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ncelsius_array = np.array([0, 20, 25, 30])\nfahrenheit_array = (celsius_array * 9\/5) + 32\nprint(f&quot;\u6444\u6c0f\u5ea6\u6570\u7ec4 {celsius_array} \u8f6c\u6362\u4e3a\u534e\u6c0f\u5ea6\u6570\u7ec4\u662f {fahrenheit_array}&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6444\u6c0f\u5ea6\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6444\u6c0f\u5ea6\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8ba1\u7b97\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u7ed3\u679c\u6e05\u6670\u6613\u8bfb\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">celsius = float(input(&quot;\u8bf7\u8f93\u5165\u6444\u6c0f\u5ea6: &quot;))\nfahrenheit = (celsius * 9\/5) + 32\nprint(f&quot;\u6444\u6c0f\u5ea6 {celsius} \u5bf9\u5e94\u7684\u534e\u6c0f\u5ea6\u662f {fahrenheit:.2f}&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6444\u6c0f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\u548c\u5904\u7406\u3002\u4f7f\u7528\u9002\u5f53\u7684\u53d8\u91cf\u540d\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5229\u7528\u6e29\u5ea6\u8f6c\u6362\u51fd\u6570\u7b49\u662f\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":1026305,"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\/1026299"}],"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=1026299"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026299\/revisions"}],"predecessor-version":[{"id":1026306,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026299\/revisions\/1026306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026305"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}