{"id":1124771,"date":"2025-01-08T19:44:52","date_gmt":"2025-01-08T11:44:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124771.html"},"modified":"2025-01-08T19:44:55","modified_gmt":"2025-01-08T11:44:55","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%8f%98%e9%87%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124771.html","title":{"rendered":"python\u5982\u4f55\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085657\/6b34eb2c-171e-4d5e-adbc-fed65bb31db2.webp\" alt=\"python\u5982\u4f55\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python \u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 str() \u51fd\u6570\u3001repr() \u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001f-strings\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f <strong>str() \u51fd\u6570<\/strong>\uff0c\u5b83\u53ef\u4ee5\u5c06\u4efb\u610f\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 str() \u51fd\u6570<\/h2>\n<\/p>\n<p><p>str() \u51fd\u6570\u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u5b83\u51e0\u4e4e\u53ef\u4ee5\u5c06\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002\u5176\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 str() \u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>number_str = str(number)<\/p>\n<p>print(number_str)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p>print(type(number_str))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6574\u6570\u53d8\u91cf number \u88ab\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf number_str\u3002\u901a\u8fc7\u6253\u5370\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\uff0cnumber_str \u7684\u7c7b\u578b\u5df2\u7ecf\u53d8\u6210\u4e86\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>str() \u51fd\u6570\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06\u590d\u6742\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u5178\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 str() \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_str = str(data)<\/p>\n<p>print(data_str)  # \u8f93\u51fa: &quot;{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 repr() \u51fd\u6570<\/h2>\n<\/p>\n<p><p>repr() \u51fd\u6570\u4e0e str() \u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u7684\u4e3b\u8981\u7528\u9014\u662f\u751f\u6210\u4e00\u4e2a\u89e3\u91ca\u5668\u53ef\u8bfb\u7684\u8868\u8fbe\u5f62\u5f0f\u3002\u6362\u53e5\u8bdd\u8bf4\uff0crepr() \u51fd\u6570\u901a\u5e38\u8fd4\u56de\u4e00\u4e2a\u66f4\u7cbe\u786e\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u9002\u5408\u8c03\u8bd5\u548c\u5f00\u53d1\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>number_repr = repr(number)<\/p>\n<p>print(number_repr)  # \u8f93\u51fa: &#39;123&#39;<\/p>\n<p>print(type(number_repr))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e str() \u51fd\u6570\u7c7b\u4f3c\uff0crepr() \u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u6574\u6570\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0crepr() \u51fd\u6570\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u66f4\u9002\u5408\u5728\u89e3\u91ca\u5668\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>repr() \u51fd\u6570\u5728\u8c03\u8bd5\u548c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u67e5\u770b\u5bf9\u8c61\u7684\u5185\u90e8\u7ed3\u6784\u65f6\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u8c03\u8bd5\u4e00\u4e2a\u590d\u6742\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 repr() \u51fd\u6570\u751f\u6210\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u5176\u5185\u90e8\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_repr = repr(data)<\/p>\n<p>print(data_repr)  # \u8f93\u51fa: &quot;{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f20\u7edf\u7684 % \u683c\u5f0f\u5316\u3001str.format() \u65b9\u6cd5\u4ee5\u53ca f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1. % \u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>% \u683c\u5f0f\u5316\u662f Python \u4e2d\u6700\u65e9\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8e C \u8bed\u8a00\u4e2d\u7684 printf \u51fd\u6570\u3002\u5b83\u901a\u8fc7 % \u64cd\u4f5c\u7b26\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>info = &quot;Name: %s, Age: %d&quot; % (name, age)<\/p>\n<p>print(info)  # \u8f93\u51fa: &quot;Name: Alice, Age: 25&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c%s \u548c %d \u5206\u522b\u7528\u4e8e\u5b57\u7b26\u4e32\u548c\u6574\u6570\u7684\u683c\u5f0f\u5316\uff0c\u5e76\u5c06\u53d8\u91cf name \u548c age \u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>2. str.format() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>str.format() \u65b9\u6cd5\u662f Python 2.7 \u548c 3.0 \u5f15\u5165\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u6bd4 % \u683c\u5f0f\u5316\u66f4\u7075\u6d3b\u3001\u66f4\u5f3a\u5927\u3002\u901a\u8fc7\u4f7f\u7528 {} \u5360\u4f4d\u7b26\u548c format() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>info = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p>print(info)  # \u8f93\u51fa: &quot;Name: Alice, Age: 25&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c{} \u5360\u4f4d\u7b26\u7528\u4e8e\u63d2\u5165\u53d8\u91cf\uff0cformat() \u65b9\u6cd5\u5c06\u53d8\u91cf name \u548c age \u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3. f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h3>\n<\/p>\n<p><p>f-strings \u662f Python 3.6 \u5f15\u5165\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0 f \u5b57\u6bcd\uff0c\u5e76\u5728 {} \u4e2d\u63d2\u5165\u53d8\u91cf\u540d\uff0c\u53ef\u4ee5\u975e\u5e38\u7b80\u6d01\u5730\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(info)  # \u8f93\u51fa: &quot;Name: Alice, Age: 25&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cf \u5b57\u7b26\u4e32\u901a\u8fc7\u5728 {} \u4e2d\u63d2\u5165\u53d8\u91cf\u540d\uff0c\u5c06\u53d8\u91cf name \u548c age \u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u9002\u7528\u4e8e\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u751f\u6210\u52a8\u6001\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 f-strings \u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(info)  # \u8f93\u51fa: &quot;Name: Alice, Age: 25&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528 join() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>join() \u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5\u8d77\u6765\u3002join() \u65b9\u6cd5\u5728\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u7e41\u7410\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p>sentence = &#39; &#39;.join(words)<\/p>\n<p>print(sentence)  # \u8f93\u51fa: &quot;Hello World&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cjoin() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868 words \u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32 sentence\uff0c\u5e76\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>join() \u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5355\u8bcd\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u53e5\u5b50\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 join() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p>sentence = &#39; &#39;.join(words)<\/p>\n<p>print(sentence)  # \u8f93\u51fa: &quot;Hello World&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528 json.dumps() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>json.dumps() \u65b9\u6cd5\u662f Python \u7684 json \u6a21\u5757\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u7279\u522b\u9002\u5408\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5b57\u5178\u548c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_json = json.dumps(data)<\/p>\n<p>print(data_json)  # \u8f93\u51fa: &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cjson.dumps() \u65b9\u6cd5\u5c06\u5b57\u5178 data \u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32 data_json\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>json.dumps() \u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u4e0e Web \u670d\u52a1\u4ea4\u4e92\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u5178\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5e76\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 json.dumps() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_json = json.dumps(data)<\/p>\n<p>print(data_json)  # \u8f93\u51fa: &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\u53ef\u4ee5\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u6216\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528 ast.literal_eval() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>ast.literal_eval() \u65b9\u6cd5\u662f Python \u7684 ast \u6a21\u5757\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5b89\u5168\u5730\u8bc4\u4f30\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684 Python \u8868\u8fbe\u5f0f\u3002\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684 Python \u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>data_str = &quot;{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}&quot;<\/p>\n<p>data = ast.literal_eval(data_str)<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>print(type(data))  # \u8f93\u51fa: &lt;class &#39;dict&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cast.literal_eval() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32 data_str \u8f6c\u6362\u4e3a\u5b57\u5178 data\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>ast.literal_eval() \u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5b89\u5168\u5730\u8bc4\u4f30\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684 Python \u8868\u8fbe\u5f0f\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u4e0d\u53ef\u4fe1\u8f93\u5165\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u5b57\u7b26\u4e32\u7684\u7528\u6237\u8f93\u5165\u8f6c\u6362\u4e3a\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 ast.literal_eval() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>data_str = &quot;{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}&quot;<\/p>\n<p>data = ast.literal_eval(data_str)<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>print(type(data))  # \u8f93\u51fa: &lt;class &#39;dict&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528 pickle.dumps() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>pickle.dumps() \u65b9\u6cd5\u662f Python \u7684 pickle \u6a21\u5757\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u7279\u522b\u9002\u5408\u5c06\u590d\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u4f20\u8f93\u5230\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_pickle = pickle.dumps(data)<\/p>\n<p>print(data_pickle)  # \u8f93\u51fa: b&#39;\\x80\\x03}q\\x00(X\\x04\\x00\\x00\\x00nameq\\x01X\\x05\\x00\\x00\\x00Aliceq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x19u.&#39;<\/p>\n<p>print(type(data_pickle))  # \u8f93\u51fa: &lt;class &#39;bytes&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cpickle.dumps() \u65b9\u6cd5\u5c06\u5b57\u5178 data \u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u5b57\u7b26\u4e32 data_pickle\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>pickle.dumps() \u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u590d\u6742\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u4f20\u8f93\u5230\u7f51\u7edc\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5305\u542b\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u5bf9\u8c61\u4fdd\u5b58\u5230\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 pickle.dumps() \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>data_pickle = pickle.dumps(data)<\/p>\n<p>print(data_pickle)  # \u8f93\u51fa: b&#39;\\x80\\x03}q\\x00(X\\x04\\x00\\x00\\x00nameq\\x01X\\x05\\x00\\x00\\x00Aliceq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x19u.&#39;<\/p>\n<p>print(type(data_pickle))  # \u8f93\u51fa: &lt;class &#39;bytes&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002<strong>str() \u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u7c7b\u578b\u8f6c\u6362\uff1b<strong>repr() \u51fd\u6570<\/strong>\u9002\u7528\u4e8e\u8c03\u8bd5\u548c\u5f00\u53d1\u65f6\u751f\u6210\u89e3\u91ca\u5668\u53ef\u8bfb\u7684\u8868\u8fbe\u5f62\u5f0f\uff1b<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\uff08\u5305\u62ec % \u683c\u5f0f\u5316\u3001str.format() \u65b9\u6cd5\u548c f-strings\uff09\u9002\u7528\u4e8e\u751f\u6210\u52a8\u6001\u5185\u5bb9\uff1b<strong>join() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1b<strong>json.dumps() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1b<strong>ast.literal_eval() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u5b89\u5168\u5730\u8bc4\u4f30\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684 Python \u8868\u8fbe\u5f0f\uff1b<strong>pickle.dumps() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u5c06\u590d\u6742\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u4f20\u8f93\u5230\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u5c06\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u5305\u62ec\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u4f8b\u5982\uff0c<code>str(123)<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\uff0c\u800c<code>str([1, 2, 3])<\/code>\u5c06\u8fd4\u56de<code>&quot;[1, 2, 3]&quot;<\/code>\u3002\u8fd9\u79cd\u8f6c\u6362\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6587\u672c\u8f93\u51fa\u65f6\u3002<\/p>\n<p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u6216<code>format()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u53d8\u91cf\u4e3a\u5b57\u7b26\u4e32\u65f6\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6bd4\u5982\uff0c\u4f7f\u7528f-string\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>name = &quot;Alice&quot;; age = 30; result = f&quot;My name is {name} and I am {age} years old.&quot;<\/code>\u3002\u8fd9\u6837\u4e0d\u4ec5\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u4e5f\u80fd\u65b9\u4fbf\u5730\u63d2\u5165\u53d8\u91cf\u503c\uff0c\u4f7f\u8f93\u51fa\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u7279\u5b9a\u60c5\u51b5\u4e0b\u9700\u8981\u6ce8\u610f\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5c06\u5927\u578b\u6570\u636e\u7ed3\u6784\uff08\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8f93\u51fa\u975e\u5e38\u5197\u957f\u3002\u56e0\u6b64\uff0c\u8003\u8651\u4f7f\u7528JSON\u683c\u5f0f\u5316\u5de5\u5177\uff08\u5982<code>json.dumps()<\/code>\uff09\u6765\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u548c\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u9700\u8981\u786e\u4fdd\u5b9e\u73b0<code>__str__<\/code>\u6216<code>__repr__<\/code>\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u80fd\u6b63\u786e\u53cd\u6620\u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 str() \u51fd\u6570\u3001repr() \u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001f- [&hellip;]","protected":false},"author":3,"featured_media":1124779,"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\/1124771"}],"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=1124771"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124771\/revisions"}],"predecessor-version":[{"id":1124781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124771\/revisions\/1124781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}