{"id":174793,"date":"2024-05-08T18:38:10","date_gmt":"2024-05-08T10:38:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174793.html"},"modified":"2024-05-08T18:38:16","modified_gmt":"2024-05-08T10:38:16","slug":"boostpythondict%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e4%b8%ad%e6%96%87%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174793.html","title":{"rendered":"boost::python::dict\u5982\u4f55\u5904\u7406\u4e2d\u6587\u503c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052434\/4a80398c-9a4c-4a8b-9671-b49120a1b05e.webp\" alt=\"boost::python::dict\u5982\u4f55\u5904\u7406\u4e2d\u6587\u503c\" \/><\/p>\n<p><p><strong>boost::python::dict \u5904\u7406\u4e2d\u6587\u503c\u4e3b\u8981\u6d89\u53ca\u5230\u7f16\u7801\u8f6c\u6362\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u8868\u793a\u4e2d\u6587\u3001\u4ee5\u53ca\u5728\u4ea4\u4e92\u4e2d\u786e\u4fdd\u7f16\u7801\u4e00\u81f4\u6027<\/strong>\u3002\u5728Python 2\u4e2d\uff0c\u4e2d\u6587\u5b57\u7b26\u4e32\u53ef\u80fd\u9700\u8981\u660e\u786e\u5730\u8f6c\u4e3aUnicode\u683c\u5f0f\u6765\u4fdd\u8bc1\u6b63\u786e\u5904\u7406\uff1b\u800c\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u5df2\u7ecf\u662fUnicode\uff0c\u7b80\u5316\u4e86\u5904\u7406\u6d41\u7a0b\u3002\u4e3a\u4e86\u6b63\u786e\u5904\u7406\u4e2d\u6587\u503c\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u4e32\u4ee5Unicode\u5f62\u5f0f\u5b58\u5728\uff0c\u63a5\u7740\u901a\u8fc7\u8bbe\u7f6e\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\uff09\u8fdb\u884c\u5b58\u53d6\uff0c\u6700\u540e\u5728C++\u548cPython\u4e4b\u95f4\u4ea4\u4e92\u65f6\u6ce8\u610f\u7f16\u7801\u7684\u8f6c\u6362\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u4e32\u4ee5\u6b63\u786e\u5f62\u5f0f\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u503c\u65f6\uff0c<strong>\u9996\u8981\u4efb\u52a1\u662f\u4fdd\u8bc1\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4ee5\u6b63\u786e\u7684\u5f62\u5f0f\u5b58\u5728<\/strong>\u3002\u5728Python 3\u4e2d\uff0c\u6240\u6709\u7684\u5b57\u7b26\u4e32\u9ed8\u8ba4\u5df2\u7ecf\u662fUnicode\u683c\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u800c\u5728Python 2\u4e2d\uff0c\u5219\u9700\u786e\u4fdd\u5b57\u7b26\u4e32\u88ab\u8f6c\u6362\u6210Unicode\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u7f16\u7801\u76f8\u5173\u7684\u9519\u8bef\u3002\u5bf9\u4e8e\u4ece\u5176\u4ed6\u7f16\u7801\u83b7\u53d6\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u4ece\u6587\u4ef6\u6216\u7f51\u7edc\u4e2d\u8bfb\u53d6\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5e94\u5f53\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u8fdb\u884c\u89e3\u7801\u6210Unicode\u3002Unicode\u4f5c\u4e3a\u4e00\u79cd\u5305\u5bb9\u6027\u6781\u5f3a\u7684\u5b57\u7b26\u8868\u793a\u65b9\u5f0f\uff0c\u80fd\u591f\u5f88\u597d\u5730\u5904\u7406\u5404\u79cd\u56fd\u9645\u5316\u5b57\u7b26\uff0c\u5305\u62ec\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u4e2d\u6587\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u663e\u5f0f\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUnicode\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5728Python 2\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0<code>u<\/code>\u524d\u7f00\u6216\u8005\u4f7f\u7528<code>.decode(&#039;\u7f16\u7801&#039;)<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chinese_text = u&#039;\u8fd9\u662f\u4e2d\u6587&#039;<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>chinese_text = &#039;\u8fd9\u662f\u4e2d\u6587&#039;.decode(&#039;utf-8&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u786e\u4fdd\u4e2d\u6587\u4ee5Unicode\u5f62\u5f0f\u5b58\u5728\u540e\uff0c\u4e0b\u4e00\u6b65\u662f<strong>\u5173\u6ce8\u5728\u5b58\u53d6\u548c\u4ea4\u4e92\u65f6\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f<\/strong>\u3002UTF-8\u4f5c\u4e3a\u4e00\u79cd\u5bf9\u4e2d\u6587\u5b57\u7b26\u53cb\u597d\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u662f\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u9996\u9009\u3002\u5728C++\u548cPython\u4e4b\u95f4\u4ea4\u4e92\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06Unicode\u5b57\u7b26\u4e32\u7f16\u7801\u6210UTF-8\u683c\u5f0f\u7684\u5b57\u8282\u4e32\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.encode(&#039;utf-8&#039;)<\/code>\u65b9\u6cd5\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u6210UTF-8\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">utf8_encoded = chinese_text.encode(&#039;utf-8&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5c06\u4e2d\u6587\u503c\u5199\u5165\u6587\u4ef6\u6216\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u65f6\uff0c\u4e5f\u5e94\u8be5\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4ee5\u907f\u514d\u7f16\u7801\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406C++\u4e0ePython\u4e4b\u95f4\u7684\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>boost::python::dict<\/code>\u4e0ePython\u4ea4\u4e92\u65f6\uff0c\u5904\u7406\u4e2d\u6587\u503c\u7684\u5173\u952e\u5728\u4e8e<strong>\u4fdd\u6301C++\u5c42\u9762\u4e0ePython\u5c42\u9762\u7684\u7f16\u7801\u4e00\u81f4<\/strong>\u3002\u5728C++\u4e2d\u64cd\u4f5c\u5b57\u7b26\u4e32\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u4e0ePython\u7aef\u4e00\u81f4\u7684\u7f16\u7801\uff08\u5982UTF-8\uff09\u3002\u5728\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9Python\u4e4b\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7Boost.Python\u63d0\u4f9b\u7684\u63a5\u53e3\u5c06C++\u5b57\u7b26\u4e32\u8f6c\u6362\u6210Python\u8ba4\u8bc6\u7684Unicode\u5bf9\u8c61\uff0c\u6216\u8005\u901a\u8fc7\u5148\u5c06C++\u5b57\u7b26\u4e32\u7f16\u7801\u6210\u5b57\u8282\u6570\u7ec4\u7136\u540e\u5728Python\u7aef\u89e3\u7801\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u6784\u9020\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u7684<code>boost::python::dict<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;boost\/python.hpp&gt;<\/p>\n<p>boost::python::dict createDictWithChinese() {<\/p>\n<p>    boost::python::dict d;<\/p>\n<p>    d[&quot;key&quot;] = boost::python::str(&quot;\u8fd9\u662f\u4e2d\u6587\u503c&quot;.encode(&quot;utf-8&quot;));<\/p>\n<p>    return d;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u7aef\uff0c\u9700\u8981\u6b63\u786e\u89e3\u7801\u8fd9\u4e9b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = createDictWithChinese()<\/p>\n<p>print(d[&quot;key&quot;].decode(&quot;utf-8&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u793a\u4f8b\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u901a\u8fc7\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u6765\u5f52\u7eb3\u4e0a\u8ff0\u6240\u6709\u73af\u8282\u7684\u6b63\u786e\u505a\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5728C++\u4e2d\u521b\u5efa\u4e00\u4e2a<code>boost::python::dict<\/code>\uff0c\u5b83\u5305\u542b\u4e86\u4e2d\u6587\u952e\u503c\u5bf9\uff0c\u5e76\u5728Python\u811a\u672c\u4e2d\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u4e2d\u6587\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u786e\u4fdd\u7f16\u7801\u5728\u6574\u4e2a\u6d41\u7a0b\u4e2d\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728C++\u90e8\u5206<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;boost\/python.hpp&gt;<\/p>\n<p>boost::python::dict createDictWithChinese() {<\/p>\n<p>    boost::python::dict d;<\/p>\n<p>    \/\/ \u5047\u8bbe\u7f16\u8bd1\u5668\u652f\u6301C++11\u7684unicode\u5b57\u7b26\u4e32<\/p>\n<p>    auto chinese_text = u8&quot;\u952e\u4e3a\u4e2d\u6587, \u503c\u4e5f\u662f\u4e2d\u6587&quot;;<\/p>\n<p>    d[chinese_text] = chinese_text;<\/p>\n<p>    return d;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728Python\u90e8\u5206<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = createDictWithChinese()<\/p>\n<p>for key, value in d.items():<\/p>\n<p>    print(key.decode(&#039;utf-8&#039;), &#039;:&#039;, value.decode(&#039;utf-8&#039;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u6b63\u786e\u5904\u7406<code>boost::python::dict<\/code>\u4e2d\u7684\u4e2d\u6587\u503c\u6d89\u53ca\u5230\u5bf9\u5b57\u7b26\u4e32\u7684Unicode\u8868\u793a\u3001\u7f16\u89e3\u7801\u7684\u6b63\u786e\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5728C++\u4e0ePython\u4ea4\u4e92\u65f6\u6ce8\u610f\u4fdd\u6301\u7f16\u7801\u4e00\u81f4\u6027\u3002\u6309\u7167\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5904\u7406\u4e2d\u6587\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728boost::python::dict\u4e2d\u53ef\u4ee5\u76f4\u63a5\u5904\u7406\u4e2d\u6587\u503c\u5417\uff1f<\/strong><br \/>\n\u5728boost::python::dict\u4e2d\u76f4\u63a5\u5904\u7406\u4e2d\u6587\u503c\u662f\u53ef\u884c\u7684\u3002Boost.Python\u5e93\u63d0\u4f9b\u4e86\u5bf9C++\u548cPython\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u7684\u652f\u6301\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728C++\u4ee3\u7801\u4e2d\u4f7f\u7528Python\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5728boost::python::dict\u4e2d\u5b58\u50a8\u548c\u5904\u7406\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u503c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728boost::python::dict\u4e2d\u4f7f\u7528\u4e2d\u6587\u503c\u8fdb\u884c\u64cd\u4f5c\uff1f<\/strong><br \/>\n\u4f7f\u7528\u4e2d\u6587\u503c\u5728boost::python::dict\u4e2d\u8fdb\u884c\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e0e\u4f7f\u7528\u82f1\u6587\u503c\u7c7b\u4f3c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u5c06\u4e2d\u6587\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\uff0c\u4e0e\u76f8\u5e94\u7684\u503c\u4e00\u8d77\u5b58\u50a8\u5230dict\u4e2d\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528boost::python::dict\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u8bbf\u95ee\u3001\u66f4\u65b0\u548c\u5220\u9664\u8fd9\u4e9b\u4e2d\u6587\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>3. \u5728\u4f7f\u7528\u4e2d\u6587\u503c\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\n\u4f7f\u7528\u4e2d\u6587\u503c\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u7f16\u7801\u517c\u5bb9\u6027\u3002Boost.Python\u5e93\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\u6216\u503c\uff0c\u9700\u8981\u786e\u4fdd\u5b83\u4eec\u7684\u7f16\u7801\u4e5f\u662fUTF-8\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u5728\u5904\u7406\u4e2d\u6587\u503c\u65f6\u9075\u5faaPython\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u89c4\u5219\uff0c\u5982\u5408\u9002\u5730\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u548c\u76f8\u5173\u7684\u7f16\u7801\/\u89e3\u7801\u51fd\u6570\u6765\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728boost::python::dict\u4e2d\u6b63\u786e\u5904\u7406\u548c\u64cd\u4f5c\u4e2d\u6587\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"boost::python::dict \u5904\u7406\u4e2d\u6587\u503c\u4e3b\u8981\u6d89\u53ca\u5230\u7f16\u7801\u8f6c\u6362\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u8868\u793a\u4e2d\u6587\u3001\u4ee5\u53ca\u5728 [&hellip;]","protected":false},"author":3,"featured_media":174803,"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\/174793"}],"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=174793"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174803"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}