{"id":1318,"date":"2023-06-27T09:07:28","date_gmt":"2023-06-27T01:07:28","guid":{"rendered":"https:\/\/program.snlcw.com\/?p=1318"},"modified":"2026-04-09T17:29:20","modified_gmt":"2026-04-09T09:29:20","slug":"python-%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/program.snlcw.com\/1318.html","title":{"rendered":"Python \u7b14\u8bb0"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/program.snlcw.com\/1318.html\/#Ubuntu_%E6%BA%90%E4%B8%8E_Python_%E7%89%88%E6%9C%AC%E5%85%B3%E7%B3%BB\" >Ubuntu \u6e90\u4e0e Python \u7248\u672c\u5173\u7cfb<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/program.snlcw.com\/1318.html\/#PyCharm_%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%89%A7%E8%A1%8C_pip_%E8%A6%81%E7%94%A8%E5%9B%BD%E5%86%85%E6%BA%90\" >PyCharm \u547d\u4ee4\u884c\u6267\u884c pip \u8981\u7528\u56fd\u5185\u6e90<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/program.snlcw.com\/1318.html\/#%E5%AF%BC%E5%87%BA%E4%BE%9D%E8%B5%96%E5%88%B0_requirementstx\" >\u5bfc\u51fa\u4f9d\u8d56\u5230 requirements.tx<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/program.snlcw.com\/1318.html\/#%E5%AF%BC%E5%87%BA%E5%AE%89%E8%A3%85%E7%A6%BB%E7%BA%BF%E5%8C%85\" >\u5bfc\u51fa\/\u5b89\u88c5\u79bb\u7ebf\u5305<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/program.snlcw.com\/1318.html\/#Nuitka_%E4%BB%A3%E7%A0%81%E4%BF%9D%E6%8A%A4\" >Nuitka \u4ee3\u7801\u4fdd\u62a4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/program.snlcw.com\/1318.html\/#Python27%E7%BC%96%E7%A0%81%E5%9F%BA%E7%A1%80\" >Python2.7\u7f16\u7801\u57fa\u7840<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/program.snlcw.com\/1318.html\/#encodedecodeunicodepython%E8%84%9A%E6%9C%AC%E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81\" >encode\/decode\/unicode\/python\u811a\u672c\u6587\u4ef6\u7f16\u7801<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/program.snlcw.com\/1318.html\/#%E5%AE%A2%E6%88%B7%E7%8E%B0%E5%9C%BA%E5%A4%9A%E5%8F%B0%E5%A4%A9%E8%9E%8D%E4%BF%A1%E8%AE%BE%E5%A4%87%E9%87%87%E9%9B%86%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98\" >\u5ba2\u6237\u73b0\u573a\u591a\u53f0\u5929\u878d\u4fe1\u8bbe\u5907\u91c7\u96c6\u4e71\u7801\u95ee\u9898<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ubuntu_%E6%BA%90%E4%B8%8E_Python_%E7%89%88%E6%9C%AC%E5%85%B3%E7%B3%BB\"><\/span>Ubuntu \u6e90\u4e0e Python \u7248\u672c\u5173\u7cfb<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ubuntu18.04 \u6e90\u9ed8\u8ba4 python3.6<\/li>\n\n\n\n<li>ubuntu20.04 \u6e90\u9ed8\u8ba4 python3.8<\/li>\n\n\n\n<li>ubuntu22.04 \u6e90\u9ed8\u8ba4 python3.10<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PyCharm_%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%89%A7%E8%A1%8C_pip_%E8%A6%81%E7%94%A8%E5%9B%BD%E5%86%85%E6%BA%90\"><\/span>PyCharm \u547d\u4ee4\u884c\u6267\u884c pip \u8981\u7528\u56fd\u5185\u6e90<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4f8b\u5982\u5b89\u88c5 torch \u53ef\u4ee5\u4f7f\u7528\u6e05\u534e\u6e90 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">-i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/mark><\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip3 install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 xformers --index-url https:\/\/download.pytorch.org\/whl\/cu118 -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/pre>\n\n\n\n<p>\u4e0d\u7528\u56fd\u5185\u6e90\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('&lt;pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000025A92386740>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')': \/torch\/<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AF%BC%E5%87%BA%E4%BE%9D%E8%B5%96%E5%88%B0_requirementstx\"><\/span>\u5bfc\u51fa\u4f9d\u8d56\u5230 requirements.tx<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u63a8\u8350\u4f7f\u7528 pipreqs \u66ff\u4ee3 pip freeze\uff0c\u5b83\u901a\u8fc7\u626b\u63cf\u6e90\u7801\u4e2d\u7684 import \u8bed\u53e5\u6765\u751f\u6210\u4f9d\u8d56\u5217\u8868\uff0c\u80fd\u6709\u6548\u5254\u9664\u672a\u4f7f\u7528\u7684\u5e93\n# \u53c2\u6570\u89e3\u91ca\uff1a\n# .\/\uff1a\u626b\u63cf\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6e90\u4ee3\u7801\u3002\n# --encoding=utf-8\uff1a\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u9632\u6b62\u4e2d\u6587\u6ce8\u91ca\u62a5\u9519\u3002\n# --ignore .venv\uff1a\u5173\u952e\u53c2\u6570\u3002\u6392\u9664\u865a\u62df\u73af\u5883\u76ee\u5f55\uff0c\u907f\u514d\u626b\u63cf\u5230\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u6d77\u91cf\u7b2c\u4e09\u65b9\u5e93\u3002\n# --force\uff1a\u5f3a\u5236\u8986\u76d6\u5df2\u5b58\u5728\u7684 requirements.txt \u6587\u4ef6\u3002\npipreqs .\/ --encoding=utf-8 --ignore .venv --force\n\n# \u4f1a\u628a\u5f53\u524d\u865a\u62df\u73af\u5883\u4e2d\u6240\u6709\u5b89\u88c5\u8fc7\u7684\u5e93\u5168\u5bfc\u51fa\u6765\uff0c\u5305\u62ec\u90a3\u4e9b\u4f60\u66fe\u7ecf\u6d4b\u8bd5\u8fc7\u4f46\u6700\u7ec8\u6ca1\u7528\u4e0a\u7684\u5e93\uff0c\u751a\u81f3\u5305\u62ec\u4e00\u4e9b\u5f00\u53d1\u5de5\u5177\npip freeze > requirements.txt<\/pre>\n\n\n\n<p><strong>\u907f\u5751\u6307\u5357\uff1a<\/strong><br>pipreqs \u5de5\u5177\u65e0\u6cd5\u68c0\u6d4b\u9690\u5f0f\u4f9d\u8d56\uff08\u5373\u4ee3\u7801\u4e2d\u672a\u76f4\u63a5 import \u4f46\u8fd0\u884c\u5fc5\u987b\u7684\u5e93\uff0c\u5982 gunicorn\u3001uvicorn \u6216\u67d0\u4e9b\u6570\u636e\u5e93\u9a71\u52a8\uff09\u3002\u751f\u6210\u6587\u4ef6\u540e\uff0c\u8bf7\u52a1\u5fc5\u68c0\u67e5\u5e76\u624b\u52a8\u8865\u5168\u8fd9\u4e9b\u5e93\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AF%BC%E5%87%BA%E5%AE%89%E8%A3%85%E7%A6%BB%E7%BA%BF%E5%8C%85\"><\/span>\u5bfc\u51fa\/\u5b89\u88c5\u79bb\u7ebf\u5305<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e00\u822c\u7528\u4e8e\u5ba2\u6237\u7684\u751f\u4ea7\u73af\u5883\u6ca1\u6709\u4e92\u8054\u7f51\uff0c\u65e0\u6cd5\u5728\u73b0\u573a pip install\uff0c\u53ea\u80fd\u628a\u4f9d\u8d56\u5305\u63d0\u524d\u4e0b\u8f7d\u6210 .whl \u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5bfc\u51fa\u79bb\u7ebf\u5305\u5230 packages \u76ee\u5f55\u3002\n# \u6ce8\uff1a\u9700\u6839\u636e\u4f60\u7cfb\u7edf\u7684 Python \u7248\u672c\u66ff\u6362 3.11.2\uff0c\u6839\u636e\u67b6\u6784\u66ff\u6362 x86_64 \u6216 aarch64\u3002\npip download -r requirements.txt -d .\/packages --platform manylinux2014_x86_64 --python-version 3.11.2 --only-binary=:all:\n\n# \u5b89\u88c5\u79bb\u7ebf\u5305\uff08\u544a\u8bc9 pip \u4ece\u672c\u5730\u6587\u4ef6\u5939\u5b89\u88c5\uff0c\u4e0d\u53bb\u8bf7\u6c42\u7f51\u7edc\uff09\npip install --no-cache-dir --no-index --find-links=.\/packages -r requirements.txt<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nuitka_%E4%BB%A3%E7%A0%81%E4%BF%9D%E6%8A%A4\"><\/span>Nuitka \u4ee3\u7801\u4fdd\u62a4<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Nuitka\u662f\u4e00\u4e2a\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210\u9ad8\u6548\u539f\u751f\u673a\u5668\u7801\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u5982Windows\u4e0a\u7684.exe\uff09\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u5c06&nbsp;src\/main\/python2\/&nbsp;\u76ee\u5f55\u4e0b\u7684&nbsp;.py&nbsp;\u6587\u4ef6\uff0c\u901a\u8fc7 Nuitka \u7f16\u8bd1\u5668\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u7684&nbsp;.so&nbsp;\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u7f16\u8bd1\u597d\u7684\u6587\u4ef6\u653e\u56de&nbsp;src\/main\/resources\/python2\/&nbsp;\u76ee\u5f55\u3002\u8fd9\u6837\u505a\u7684\u4e3b\u8981\u539f\u56e0\u901a\u5e38\u6709\u4e24\u4e2a\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u4fdd\u62a4\/\u6df7\u6dc6<\/strong>\uff1a.so&nbsp;\u6587\u4ef6\u662f\u7f16\u8bd1\u540e\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6bd4\u7eaf\u6587\u672c\u7684&nbsp;.py&nbsp;\u6587\u4ef6\u66f4\u96be\u88ab\u9605\u8bfb\u548c\u53cd\u5411\u5de5\u7a0b\uff0c\u53ef\u4ee5\u6709\u6548\u4fdd\u62a4\u6e90\u4ee3\u7801\u4e2d\u7684\u77e5\u8bc6\u4ea7\u6743\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u63d0\u5347<\/strong>\uff1aNuitka \u5c06 Python \u4ee3\u7801\u7f16\u8bd1\u6210 C \u7ea7\u522b\u7684\u4ee3\u7801\uff0c\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python27%E7%BC%96%E7%A0%81%E5%9F%BA%E7%A1%80\"><\/span>Python2.7\u7f16\u7801\u57fa\u7840<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5728 Python 2.7 \u4e2d\uff0c\u5b57\u7b26\u4e32\u53d8\u91cf\u9ed8\u8ba4\u4f7f\u7528 str \u7c7b\u578b\uff0c\u800c\u4e0d\u662f unicode \u7c7b\u578b\u3002\u8fd9\u662f Python 2 \u548c Python 3 \u7684\u4e00\u4e2a\u663e\u8457\u533a\u522b\u3002\u7f51\u7edc\u5b57\u8282\u6d41\u8f6c\u6362\u6210<\/p>\n\n\n\n<p>str \u7c7b\u578b\u65f6\uff0cPython2.7 \u5e76\u4e0d\u4f1a\u81ea\u52a8\u8bc6\u522b\u6216\u5904\u7406\u7f16\u7801\u3002\u7f51\u7edc\u5b57\u8282\u6d41\u5728\u4f20\u8f93\u65f6\u53ea\u662f\u5b57\u8282\u6570\u636e\uff0c\u7f16\u7801\u65b9\u5f0f\u7531\u53d1\u9001\u65b9\u548c\u63a5\u6536\u65b9\u7ea6\u5b9a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"encodedecodeunicodepython%E8%84%9A%E6%9C%AC%E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81\"><\/span>encode\/decode\/unicode\/python\u811a\u672c\u6587\u4ef6\u7f16\u7801<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5b57\u7b26\u4e32\u901a\u8fc7\u7f16\u7801\u6210\u4e3a\u5b57\u8282\u7801\nstr1 = \"\u5f20\u4e09\"\nbytes = str1.decode(encoding=\"utf-8\")\n\n# \u5b57\u8282\u7801\u901a\u8fc7\u89e3\u7801\u6210\u4e3a\u5b57\u7b26\u4e32\nstr2 = bytes.encode(encoding=\"utf-8\")\nprint str2<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"96\" src=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627090604.png\" alt=\"\" class=\"wp-image-1319\" srcset=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627090604.png 480w, https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627090604-300x60.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/figure>\n\n\n\n<p>\u5199\u4ee3\u7801\u65f6\uff0c\u5728\u5b57\u7b26\u4e32\u524d\u52a0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">u<\/mark>\uff0c\u8868\u793a\u8fd9\u662f unicode \u5b57\u7b26\u4e32\uff08\u672a\u7f16\u7801\uff09\uff0c\u8bf4\u7684\u51c6\u786e\u70b9\u5c31\u662f\u5b57\u8282\u7801\uff0c\u7528\u5341\u516d\u8fdb\u5236\u8868\u793a\u5c31\u662f\u8fd9\u4e32<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"> \\u674e\\u56db<\/mark>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"90\" src=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20231627090604.png\" alt=\"\" class=\"wp-image-1321\" srcset=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20231627090604.png 600w, https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20231627090604-300x45.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>\u7528 unicode \u5728\u7ebf\u89e3\u7801\u5de5\u5177\u4e5f\u53ef\u4ee5\u9a8c\u8bc1\u8fd9\u70b9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"257\" src=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627092223.png\" alt=\"\" class=\"wp-image-1324\" style=\"width:630px;height:257px\" srcset=\"https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627092223.png 630w, https:\/\/program.snlcw.com\/wp-content\/uploads\/2023\/06\/\u5fae\u4fe1\u56fe\u7247_20230627092223-300x122.png 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p>\u53e6\u5916\uff0c\u5bf9\u4e8e Python2.x \u7248\u672c\u5f00\u5934\u4e00\u822c\u4f1a\u52a0 coding: utf-8\uff0c\u8868\u793a\u58f0\u660e python \u4ee3\u7801\u7684\u6587\u672c\u683c\u5f0f\u662f utf-8 \u7f16\u7801\u7684\uff0c\u5373\u544a\u8bc9 python \u89e3\u91ca\u5668\u8981\u6309\u7167 utf-8 \u7f16\u7801\u7684\u65b9\u5f0f\u6765\u8bfb\u53d6\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u5728 Python2.x \u7684\u7248\u672c\u4e2d\uff0c\u6587\u672c\u9ed8\u8ba4\u91c7\u7528\u7684\u662f ASCII \u7f16\u7801\uff0c\u800cPython3.x \u7684\u7248\u672c\u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7684\u5c31\u662f UTF-8 \u7f16\u7801\u683c\u5f0f\uff0c\u6240\u4ee5\u5c31\u4e0d\u9700\u8981\u5728\u524d\u9762\u8fdb\u884c\u58f0\u660e\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%A2%E6%88%B7%E7%8E%B0%E5%9C%BA%E5%A4%9A%E5%8F%B0%E5%A4%A9%E8%9E%8D%E4%BF%A1%E8%AE%BE%E5%A4%87%E9%87%87%E9%9B%86%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98\"><\/span>\u5ba2\u6237\u73b0\u573a\u591a\u53f0\u5929\u878d\u4fe1\u8bbe\u5907\u91c7\u96c6\u4e71\u7801\u95ee\u9898<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u91c7\u96c6\u811a\u672c\u662f python2.7\uff0c\u901a\u8fc7\u8d26\u53f71\u6267\u884c show running \u91c7\u96c6\u5230\u7684\u914d\u7f6e\u5b58\u4e8e\u53d8\u91cf var1\uff0c\u901a\u8fc7\u8d26\u53f72\u6267\u884c firewall policy show \u91c7\u96c6\u5230\u7684\u914d\u7f6e\u5b58\u4e8e\u53d8\u91cf var2\uff0c\u4e24\u4e2a\u53d8\u91cf\u9700\u8981\u5408\u5e76\u6210\u4e00\u4e2a\u7136\u540e\u4fdd\u5b58\u5230\u6587\u4ef6\u7cfb\u7edf\uff0c\u4f46 var1 \u548c var2 \u91cc\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u5374\u4e0d\u4e00\u6837\uff0c\u4e00\u4e2a\u662f gb2312\uff0c\u53e6\u4e00\u4e2a\u662futf-8\u3002\u6d4b\u8bd5\u7b2c\u4e8c\u53f0\u65f6 var1 \u548c var2 \u91cc\u7f16\u7801\u5374\u6b63\u597d\u76f8\u53cd\uff0c\u6ca1\u6709\u4efb\u4f55\u89c4\u5f8b\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u5148\u540e\u5c1d\u8bd5decode gb2312\u548cutf-8\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">\u5982\uff1avar1.decode(&#8216;gb2312&#8217;) \u548c var2.decode(&#8216;utf-8&#8217;)<\/mark>\uff09\u5230unicode \uff0c\u53ea\u8981\u5176\u4e2d\u4e00\u4e2a decode \u4e0d\u62a5\u9519\u8bf4\u660e\u5c31\u662f\u90a3\u79cd\u7f16\u7801\uff0cvar1\u548cvar2\u90fd\u662f\u8fd9\u4e48\u5904\u7406\u6765\u7edf\u4e00\u5230 unicode\uff0c\u6700\u540e\u7528\u5ba2\u6237\u524d\u7aefUI\u9009\u62e9\u7684\u7f16\u7801\u6765\u7f16\u7801unicode\uff0c\u4fdd\u5b58\u5230\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u64cd\u4f5c\u540e\uff0c\u73b0\u573a4\u53f0\u8bbe\u5907\u7684\u91c7\u96c6\u4e0d\u518d\u4e71\u7801\uff08\u5305\u62ec\u5730\u5740\u5bf9\u8c61\u540d\u7b49\u548c\u7b56\u7565\u540d\uff09\u3002\u4f46\u5269\u4f592\u53f0\u8fd8\u662f\u51fa\u73b0\u4e86\u4e71\u7801\uff0c\u7ecf\u67e5\u95ee\u9898\u51fa\u5728 show running \u7684 var1\u53d8\u91cf\uff0c\u5b83\u7684\u5b57\u7b26\u4e32\u7f16\u7801\u65e2\u4e0d\u662f utf-8\uff0c\u4e5f\u4e0d\u662f gb2312\uff0c\u56e0\u4e3a decode\u90fd\u62a5\u9519\u3002\u89e3\u51b3\u529e\u6cd5\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">var1.decode(&#8216;gb2312&#8242;,&#8217;ignore&#8217;) \uff0c<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">\u5bf9\u5c31\u662f\u52a0\u4e2a ignore<\/mark>\uff0c\u4e0d\u5e78\u7684\u662f\u91c7\u96c6\u540e\u8fd8\u662f\u4e71\u7801\uff0c\u6539\u6210 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">var1.decode(&#8216;utf-8&#8242;,&#8217;ignore&#8217;) <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">\u540e\u95ee\u9898\u5f97\u4ee5\u89e3\u51b3\u3002<\/mark>\u4e3a\u6b64\u5c06\u81ea\u5b9a\u4e49\u91c7\u96c6\u811a\u672c\u5206\u6210\u4e86&nbsp;topsec-tos-utf8.py \u548c topsec-tos-gb2312.py \u4e24\u4e2a\uff0c\u5728\u53d8\u91cf\u4e2d\u6709\u7279\u6b8a\u5b57\u7b26\u5bfc\u81f4\u65e0\u6cd5 decode \u51fa\u7f16\u7801\u65f6\uff0c\u5ba2\u6237\u53ea\u80fd\u4e24\u4e2a\u81ea\u5b9a\u4e49\u811a\u672c\u90fd\u8bd5\u4e00\u4e0b\u3002<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ubuntu \u6e90\u4e0e Python \u7248\u672c\u5173\u7cfb PyCharm \u547d\u4ee4\u884c\u6267\u884c pip \u8981\u7528\u56fd\u5185\u6e90 \u4f8b\u5982\u5b89\u88c5 torc\u2026 <span class=\"read-more\"><a href=\"https:\/\/program.snlcw.com\/1318.html\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1318","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/posts\/1318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/comments?post=1318"}],"version-history":[{"count":26,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/posts\/1318\/revisions"}],"predecessor-version":[{"id":2980,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/posts\/1318\/revisions\/2980"}],"wp:attachment":[{"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/media?parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/categories?post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/program.snlcw.com\/wp-json\/wp\/v2\/tags?post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}