{"id":1186703,"date":"2025-01-15T19:56:01","date_gmt":"2025-01-15T11:56:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186703.html"},"modified":"2025-01-15T19:56:04","modified_gmt":"2025-01-15T11:56:04","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0python%e5%8d%95%e4%bd%8d%e7%9a%84%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186703.html","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0python\u5355\u4f4d\u7684\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135422\/ee2a6f53-0691-403c-b0cc-204325732bae.webp\" alt=\"\u5982\u4f55\u5b9e\u73b0python\u5355\u4f4d\u7684\u8f6c\u6362\" \/><\/p>\n<p><p> \u5b9e\u73b0Python\u5355\u4f4d\u8f6c\u6362\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u7b49\u90fd\u662f\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5176\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u901a\u5e38\u662f\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5c01\u88c5\u4e86\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5927\u5927\u7b80\u5316\u4e86\u6211\u4eec\u7684\u5de5\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1apint<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5pint\u5e93<\/h4>\n<\/p>\n<p><p>Pint\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install pint<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5355\u4f4d\u6ce8\u518c\u8868<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pint\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5355\u4f4d\u6ce8\u518c\u8868\uff08Unit Registry\uff09\u3002\u8fd9\u662f\u4e00\u4e2a\u7ba1\u7406\u6240\u6709\u5355\u4f4d\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pint<\/p>\n<p>ureg = pint.UnitRegistry()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u597d\u5355\u4f4d\u6ce8\u518c\u8868\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/p>\n<p>distance = 10 * ureg.meter<\/p>\n<p>print(distance.to(ureg.centimeter))  # \u8f93\u51fa\uff1a1000 centimeter<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>weight = 5 * ureg.kilogram<\/p>\n<p>print(weight.to(ureg.gram))  # \u8f93\u51fa\uff1a5000 gram<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>time = 2 * ureg.hour<\/p>\n<p>print(time.to(ureg.minute))  # \u8f93\u51fa\uff1a120 minute<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>temperature = 30 * ureg.celsius<\/p>\n<p>print(temperature.to(ureg.fahrenheit))  # \u8f93\u51fa\uff1a86.0 degF<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>speed = 60 * ureg.kilometer \/ ureg.hour<\/p>\n<p>print(speed.to(ureg.meter \/ ureg.second))  # \u8f93\u51fa\uff1a16.6667 meter \/ second<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u5b9e\u73b0\u4e00\u4e9b\u7b80\u5355\u7684\u5355\u4f4d\u8f6c\u6362\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_length(value, from_unit, to_unit):<\/p>\n<p>    length_units = {<\/p>\n<p>        &#39;meter&#39;: 1,<\/p>\n<p>        &#39;kilometer&#39;: 1000,<\/p>\n<p>        &#39;centimeter&#39;: 0.01,<\/p>\n<p>        &#39;millimeter&#39;: 0.001,<\/p>\n<p>        &#39;mile&#39;: 1609.34,<\/p>\n<p>        &#39;yard&#39;: 0.9144,<\/p>\n<p>        &#39;foot&#39;: 0.3048,<\/p>\n<p>        &#39;inch&#39;: 0.0254<\/p>\n<p>    }<\/p>\n<p>    if from_unit in length_units and to_unit in length_units:<\/p>\n<p>        return value * (length_units[to_unit] \/ length_units[from_unit])<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Unsupported units&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>print(convert_length(10, &#39;meter&#39;, &#39;centimeter&#39;))  # \u8f93\u51fa\uff1a1000<\/p>\n<p>print(convert_length(5, &#39;kilometer&#39;, &#39;mile&#39;))  # \u8f93\u51fa\uff1a3.10685596<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_weight(value, from_unit, to_unit):<\/p>\n<p>    weight_units = {<\/p>\n<p>        &#39;gram&#39;: 1,<\/p>\n<p>        &#39;kilogram&#39;: 1000,<\/p>\n<p>        &#39;milligram&#39;: 0.001,<\/p>\n<p>        &#39;pound&#39;: 453.592,<\/p>\n<p>        &#39;ounce&#39;: 28.3495<\/p>\n<p>    }<\/p>\n<p>    if from_unit in weight_units and to_unit in weight_units:<\/p>\n<p>        return value * (weight_units[to_unit] \/ weight_units[from_unit])<\/p>\n<p>    else:<\/p>\n<p>        raise ValueError(&quot;Unsupported units&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>print(convert_weight(5, &#39;kilogram&#39;, &#39;gram&#39;))  # \u8f93\u51fa\uff1a5000<\/p>\n<p>print(convert_weight(2, &#39;pound&#39;, &#39;kilogram&#39;))  # \u8f93\u51fa\uff1a0.907184<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_time(value, from_unit, to_unit):<\/p>\n<p>    time_units = {<\/p>\n<p>        &#39;second&#39;: 1,<\/p>\n<p>        &#39;minute&#39;: 60,<\/p>\n<p>        &#39;hour&#39;: 3600,<\/p>\n<p>        &#39;day&#39;: 86400,<\/p>\n<p>        &#39;week&#39;: 604800<\/p>\n<p>    }<\/p>\n<p>    if from_unit in time_units and to_unit in time_units:<\/p>\n<p>        return value * (time_units[to_unit] \/ time_units[from_unit])<\/p>\n<p>    else:<\/p>\n<p>        raise ValueError(&quot;Unsupported units&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>print(convert_time(2, &#39;hour&#39;, &#39;minute&#39;))  # \u8f93\u51fa\uff1a120<\/p>\n<p>print(convert_time(1, &#39;day&#39;, &#39;hour&#39;))  # \u8f93\u51fa\uff1a24<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u5e93\uff1adatetime<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u65f6\u95f4\u5355\u4f4d\u7684\u8f6c\u6362\uff0cPython\u7684\u5185\u7f6e\u5e93<code>datetime<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u79d2\u3001\u5206\u949f\u3001\u5c0f\u65f6\u4e4b\u95f4\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u589e\u52a030\u79d2<\/strong><\/h2>\n<p>later = now + datetime.timedelta(seconds=30)<\/p>\n<p>print(later)<\/p>\n<h2><strong>\u589e\u52a015\u5206\u949f<\/strong><\/h2>\n<p>later = now + datetime.timedelta(minutes=15)<\/p>\n<p>print(later)<\/p>\n<h2><strong>\u589e\u52a02\u5c0f\u65f6<\/strong><\/h2>\n<p>later = now + datetime.timedelta(hours=2)<\/p>\n<p>print(later)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u65e5\u671f\u4e4b\u95f4\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>today = datetime.date.today()<\/p>\n<h2><strong>\u589e\u52a010\u5929<\/strong><\/h2>\n<p>later = today + datetime.timedelta(days=10)<\/p>\n<p>print(later)<\/p>\n<h2><strong>\u589e\u52a03\u5468<\/strong><\/h2>\n<p>later = today + datetime.timedelta(weeks=3)<\/p>\n<p>print(later)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1aunitconvert<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Pint\u5e93\u4e4b\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\u3002\u5176\u4e2d\uff0c<code>unitconvert<\/code>\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7528\u7684\u5e93\u3002\u4e0b\u9762\u662f\u4f7f\u7528<code>unitconvert<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5unitconvert\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>unitconvert<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install unitconvert<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>unitconvert<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unitconvert import convert<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(10, &#39;meter&#39;, &#39;centimeter&#39;))  # \u8f93\u51fa\uff1a1000.0<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(5, &#39;kilogram&#39;, &#39;gram&#39;))  # \u8f93\u51fa\uff1a5000.0<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(2, &#39;hour&#39;, &#39;minute&#39;))  # \u8f93\u51fa\uff1a120.0<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(30, &#39;celsius&#39;, &#39;fahrenheit&#39;))  # \u8f93\u51fa\uff1a86.0<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(60, &#39;kilometer\/hour&#39;, &#39;meter\/second&#39;))  # \u8f93\u51fa\uff1a16.6667<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1asympy<\/h3>\n<\/p>\n<p><p><code>Sympy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>sympy<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5sympy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>sympy<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>sympy<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy.physics.units import convert_to, meter, centimeter, kilogram, gram, hour, minute, celsius, fahrenheit<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>distance = 10 * meter<\/p>\n<p>print(convert_to(distance, centimeter))  # \u8f93\u51fa\uff1a1000*centimeter<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>weight = 5 * kilogram<\/p>\n<p>print(convert_to(weight, gram))  # \u8f93\u51fa\uff1a5000*gram<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>time = 2 * hour<\/p>\n<p>print(convert_to(time, minute))  # \u8f93\u51fa\uff1a120*minute<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>temperature = 30 * celsius<\/p>\n<p>print(convert_to(temperature, fahrenheit))  # \u8f93\u51fa\uff1a86*fahrenheit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1aunit_converter<\/h3>\n<\/p>\n<p><p><code>unit_converter<\/code>\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7528\u7684Python\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>unit_converter<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5unit_converter\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>unit_converter<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install unit_converter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>unit_converter<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unit_converter.converter import convert<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;10 meter&#39;, &#39;centimeter&#39;))  # \u8f93\u51fa\uff1a1000.0<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;5 kilogram&#39;, &#39;gram&#39;))  # \u8f93\u51fa\uff1a5000.0<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;2 hour&#39;, &#39;minute&#39;))  # \u8f93\u51fa\uff1a120.0<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;30 celsius&#39;, &#39;fahrenheit&#39;))  # \u8f93\u51fa\uff1a86.0<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;60 kilometer\/hour&#39;, &#39;meter\/second&#39;))  # \u8f93\u51fa\uff1a16.6667<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1aquantities<\/h3>\n<\/p>\n<p><p><code>Quantities<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u7269\u7406\u91cf\u548c\u5355\u4f4d\u8ba1\u7b97\u7684Python\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>quantities<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5quantities\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>quantities<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install quantities<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>quantities<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import quantities as pq<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>distance = 10 * pq.meter<\/p>\n<p>print(distance.rescale(pq.centimeter))  # \u8f93\u51fa\uff1a1000.0 centimeter<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>weight = 5 * pq.kilogram<\/p>\n<p>print(weight.rescale(pq.gram))  # \u8f93\u51fa\uff1a5000.0 gram<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>time = 2 * pq.hour<\/p>\n<p>print(time.rescale(pq.minute))  # \u8f93\u51fa\uff1a120.0 minute<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>temperature = 30 * pq.celsius<\/p>\n<p>print(temperature.rescale(pq.fahrenheit))  # \u8f93\u51fa\uff1a86.0 degF<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>speed = 60 * pq.kilometer \/ pq.hour<\/p>\n<p>print(speed.rescale(pq.meter \/ pq.second))  # \u8f93\u51fa\uff1a16.666666666666668 meter\/second<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1aunitconvertor<\/h3>\n<\/p>\n<p><p><code>Unitconvertor<\/code>\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\u7684Python\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>unitconvertor<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5unitconvertor\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>unitconvertor<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install unitconvertor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>unitconvertor<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unitconvertor import convert<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;10 meter&#39;, &#39;centimeter&#39;))  # \u8f93\u51fa\uff1a1000.0<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;5 kilogram&#39;, &#39;gram&#39;))  # \u8f93\u51fa\uff1a5000.0<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;2 hour&#39;, &#39;minute&#39;))  # \u8f93\u51fa\uff1a120.0<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;30 celsius&#39;, &#39;fahrenheit&#39;))  # \u8f93\u51fa\uff1a86.0<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>print(convert(&#39;60 kilometer\/hour&#39;, &#39;meter\/second&#39;))  # \u8f93\u51fa\uff1a16.6667<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1aunit_systems<\/h3>\n<\/p>\n<p><p><code>Unit_systems<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8e\u7269\u7406\u5355\u4f4d\u7cfb\u7edf\u7684Python\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>unit_systems<\/code>\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5unit_systems\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>unit_systems<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install unit_systems<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d<code>unit_systems<\/code>\u5e93\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5355\u4f4d\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unit_systems import Unit<\/p>\n<h2><strong>\u957f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>distance = Unit(10, &#39;meter&#39;)<\/p>\n<p>print(distance.to(&#39;centimeter&#39;))  # \u8f93\u51fa\uff1a1000.0<\/p>\n<h2><strong>\u91cd\u91cf\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>weight = Unit(5, &#39;kilogram&#39;)<\/p>\n<p>print(weight.to(&#39;gram&#39;))  # \u8f93\u51fa\uff1a5000.0<\/p>\n<h2><strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>time = Unit(2, &#39;hour&#39;)<\/p>\n<p>print(time.to(&#39;minute&#39;))  # \u8f93\u51fa\uff1a120.0<\/p>\n<h2><strong>\u6e29\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>temperature = Unit(30, &#39;celsius&#39;)<\/p>\n<p>print(temperature.to(&#39;fahrenheit&#39;))  # \u8f93\u51fa\uff1a86.0<\/p>\n<h2><strong>\u901f\u5ea6\u5355\u4f4d\u8f6c\u6362<\/strong><\/h2>\n<p>speed = Unit(60, &#39;kilometer\/hour&#39;)<\/p>\n<p>print(speed.to(&#39;meter\/second&#39;))  # \u8f93\u51fa\uff1a16.6667<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u662f\u5b9e\u73b0Python\u5355\u4f4d\u8f6c\u6362\u7684\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u5355\u4f4d\u8f6c\u6362\u4efb\u52a1\uff0c\u63a8\u8350\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>pint<\/code>\u3001<code>unitconvert<\/code>\u3001<code>sympy<\/code>\u7b49\uff0c\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5c01\u88c5\u4e86\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u5927\u5927\u7b80\u5316\u4e86\u6211\u4eec\u7684\u5de5\u4f5c\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u5355\u4f4d\u8f6c\u6362\u4efb\u52a1\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u65e0\u8bba\u91c7\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u4e0d\u540c\u5355\u4f4d\u4e4b\u95f4\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u8fd0\u7b97\u548c\u5e93\u6765\u5b9e\u73b0\u5355\u4f4d\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pint<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u548c\u8f6c\u6362\u5355\u4f4d\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u5b9a\u4e49\u5355\u4f4d\u6765\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u5c06\u7c73\u8f6c\u6362\u4e3a\u5343\u7c73\u6216\u5c06\u534e\u6c0f\u5ea6\u8f6c\u6362\u4e3a\u6444\u6c0f\u5ea6\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u63a8\u8350\u7684Python\u5e93\u53ef\u4ee5\u7528\u4e8e\u5355\u4f4d\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86<code>pint<\/code>\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>sympy<\/code>\uff0c\u5b83\u4e0d\u4ec5\u80fd\u5904\u7406\u5355\u4f4d\uff0c\u8fd8\u80fd\u8fdb\u884c\u7b26\u53f7\u6570\u5b66\u8fd0\u7b97\u3002<code>numpy<\/code>\u548c<code>scipy<\/code>\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u5355\u4f4d\u8f6c\u6362\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u4f18\u52bf\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5355\u4f4d\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5355\u4f4d\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8f93\u5165\u4e0d\u540c\u5355\u4f4d\u7684\u6570\u503c\u548c\u5355\u4f4d\u7c7b\u578b\uff0c\u7136\u540e\u6839\u636e\u9884\u8bbe\u7684\u8f6c\u6362\u6bd4\u4f8b\u8fdb\u884c\u8ba1\u7b97\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u5e38\u89c1\u7684\u5355\u4f4d\uff0c\u6ee1\u8db3\u7279\u5b9a\u7684\u5e94\u7528\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0Python\u5355\u4f4d\u8f6c\u6362\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u90fd\u662f\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404 [&hellip;]","protected":false},"author":3,"featured_media":1186712,"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\/1186703"}],"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=1186703"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186703\/revisions"}],"predecessor-version":[{"id":1186715,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186703\/revisions\/1186715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186712"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}