{"id":999570,"date":"2024-12-27T09:42:38","date_gmt":"2024-12-27T01:42:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999570.html"},"modified":"2024-12-27T09:42:40","modified_gmt":"2024-12-27T01:42:40","slug":"python%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%bc%95%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999570.html","title":{"rendered":"python\u7f16\u7a0b\u5982\u4f55\u8f93\u5165\u5f15\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074524\/3e4e216e-f922-498b-979c-fed75680ea7f.webp\" alt=\"python\u7f16\u7a0b\u5982\u4f55\u8f93\u5165\u5f15\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u7f16\u7a0b\u4e2d\u8f93\u5165\u5f15\u53f7\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5355\u5f15\u53f7\u3001\u4f7f\u7528\u53cc\u5f15\u53f7\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528\u4e09\u5f15\u53f7\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u6280\u5de7\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u6df7\u5408\u4f7f\u7528\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u7684\u60c5\u51b5\u4e0b\u3002\u901a\u8fc7\u5728\u5f15\u53f7\u524d\u6dfb\u52a0\u53cd\u659c\u6760\uff08\\\uff09\u6765\u8f6c\u4e49\u5f15\u53f7\uff0c\u53ef\u4ee5\u907f\u514d\u8bed\u6cd5\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u53cc\u5f15\u53f7\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>&quot;He s<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>d, \\&quot;Hello, World!\\&quot;&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\uff08&#39;\uff09\u6216\u53cc\u5f15\u53f7\uff08&quot;\uff09\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u3002\u8fd9\u4e24\u4e2a\u7b26\u53f7\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\uff0c\u4e3b\u8981\u53d6\u51b3\u4e8e\u4e2a\u4eba\u4e60\u60ef\u6216\u4ee3\u7801\u98ce\u683c\u8981\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string1 = &#39;Hello, World!&#39;<\/p>\n<p>string2 = &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u672c\u8eab\u5305\u542b\u4e86\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f15\u53f7\u6765\u907f\u514d\u51b2\u7a81\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_with_single_quote = &quot;It&#39;s a nice day.&quot;<\/p>\n<p>string_with_double_quote = &#39;He said, &quot;Hello!&quot;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u4e2d\u9700\u8981\u4f7f\u7528\u4e0e\u5305\u56f4\u5b57\u7b26\u4e32\u7684\u5f15\u53f7\u76f8\u540c\u7684\u5f15\u53f7\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u4f5c\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9Python\u6b63\u786e\u5730\u89e3\u6790\u5b57\u7b26\u4e32\u4e2d\u7684\u5f15\u53f7\uff0c\u800c\u4e0d\u4f1a\u56e0\u4e3a\u5f15\u53f7\u7684\u5d4c\u5957\u800c\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">escaped_single_quote = &#39;It\\&#39;s a nice day.&#39;<\/p>\n<p>escaped_double_quote = &quot;He said, \\&quot;Hello!\\&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff0cPython\u53ef\u4ee5\u6b63\u786e\u8bc6\u522b\u5f15\u53f7\u662f\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u4e09\u5f15\u53f7<\/p>\n<\/p>\n<p><p>Python\u8fd8\u652f\u6301\u4f7f\u7528\u4e09\u5f15\u53f7\uff08&#39;&#39;&#39; \u6216 &quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u6216\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u4e09\u5f15\u53f7\u4e0d\u4ec5\u53ef\u4ee5\u8ba9\u5b57\u7b26\u4e32\u8de8\u8d8a\u591a\u884c\uff0c\u8fd8\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5305\u542b\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;This is a multi-line string.<\/p>\n<p>It can contain &#39;single quotes&#39; and &quot;double quotes&quot; without any issues.&quot;&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u975e\u5e38\u9002\u5408\u7528\u4e8e\u521b\u5efa\u6587\u6863\u5b57\u7b26\u4e32\u6216\u5305\u542b\u590d\u6742\u683c\u5f0f\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528raw\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8def\u5f84\u540d\u65f6\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u8f6c\u4e49\u5b57\u7b26\u751f\u6548\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528raw\u5b57\u7b26\u4e32\uff0c\u65b9\u6cd5\u662f\u5728\u5b57\u7b26\u4e32\u7684\u5f15\u53f7\u524d\u52a0\u4e0a\u5b57\u6bcdr\u6216R\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_string = r&quot;This is a raw string with a backslash: \\path\\to\\file&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728raw\u5b57\u7b26\u4e32\u4e2d\uff0c\u53cd\u659c\u6760\u88ab\u89c6\u4e3a\u666e\u901a\u5b57\u7b26\uff0c\u56e0\u6b64\u4e0d\u4f1a\u89e6\u53d1\u8f6c\u4e49\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5f15\u53f7\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5f15\u53f7\u4e0d\u4ec5\u7528\u4e8e\u5b9a\u4e49\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u5b57\u5178\u7684\u952e\u3001JSON\u683c\u5f0f\u7684\u6570\u636e\u7b49\u3002\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5f15\u53f7\u5f62\u5f0f\u548c\u65b9\u6cd5\u662f\u786e\u4fdd\u4ee3\u7801\u6b63\u786e\u8fd0\u884c\u7684\u91cd\u8981\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li>JSON\u683c\u5f0f\u7684\u6570\u636e<\/p>\n<p>\u5728\u5904\u7406JSON\u683c\u5f0f\u7684\u6570\u636e\u65f6\uff0c\u901a\u5e38\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u8868\u793a\u952e\u548c\u503c\u3002\u8fd9\u662f\u56e0\u4e3aJSON\u6807\u51c6\u8981\u6c42\u952e\u548c\u503c\u4f7f\u7528\u53cc\u5f15\u53f7\u3002\u4f8b\u5982\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_data = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}&#39;<\/p>\n<p>parsed_data = json.loads(json_data)<\/p>\n<p>print(parsed_data[&#39;name&#39;])  # \u8f93\u51fa: John<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2<\/p>\n<p>\u5728\u52a8\u6001\u751f\u6210SQL\u67e5\u8be2\u65f6\uff0c\u6b63\u786e\u5730\u5904\u7406\u5f15\u53f7\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u4f8b\u5982\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">user_input = &quot;O&#39;Reilly&quot;<\/p>\n<p>query = f&quot;SELECT * FROM books WHERE author = &#39;{user_input}&#39;&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u6216\u53c2\u6570\u5316\u67e5\u8be2\u6765\u907f\u514d\u6ce8\u5165\u98ce\u9669<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u53cd\u659c\u6760\u901a\u5e38\u7528\u4e8e\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\uff0c\u8fd9\u4f7f\u5f97raw\u5b57\u7b26\u4e32\u6210\u4e3a\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7406\u60f3\u9009\u62e9\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\bword\\b&quot;<\/p>\n<p>text = &quot;A word of warning&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;word&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u4f7f\u7528\u5f15\u53f7\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u5b57\u7b26\u4e32\u548c\u6587\u672c\u6570\u636e\uff0c\u907f\u514d\u5e38\u89c1\u7684\u8bed\u6cd5\u9519\u8bef\uff0c\u5e76\u7f16\u5199\u51fa\u66f4\u5065\u58ee\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5355\u5f15\u53f7\uff08&#39;\uff09\u548c\u53cc\u5f15\u53f7\uff08&quot;\uff09\u90fd\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u7684\u5b57\u7b26\u4e32\u4e2d\u9700\u8981\u5305\u542b\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f15\u53f7\u6765\u5305\u56f4\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<code>string1 = &quot;He said, &#39;Hello&#39;&quot;<\/code> \u6216\u8005 <code>string2 = &#39;She replied, &quot;Hi&quot;&#39;<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5f15\u53f7\u7684\u51b2\u7a81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u8f6c\u4e49\u5f15\u53f7\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u4e0e\u5305\u56f4\u5b57\u7b26\u4e32\u76f8\u540c\u7684\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u8fdb\u884c\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a<code>string3 = &quot;He said, \\&quot;Hello\\&quot;&quot; <\/code> \u6216 <code>string4 = &#39;It\\&#39;s a sunny day&#39;<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u4e2d\u7684\u5f15\u53f7\u4e0d\u4f1a\u88ab\u8bef\u89e3\u4e3a\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u591a\u884c\u5185\u5bb9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39; \u6216 &quot;&quot;&quot;\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5f15\u53f7\u548c\u6362\u884c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;This is a string that contains &#39;single quotes&#39; and &quot;double quotes&quot;.  \nIt spans multiple lines.&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u65e2\u7b80\u6d01\u53c8\u6613\u4e8e\u9605\u8bfb\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7f16\u7a0b\u4e2d\u8f93\u5165\u5f15\u53f7\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5355\u5f15\u53f7\u3001\u4f7f\u7528\u53cc\u5f15\u53f7\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528\u4e09\u5f15\u53f7\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u662f [&hellip;]","protected":false},"author":3,"featured_media":999577,"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\/999570"}],"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=999570"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999570\/revisions"}],"predecessor-version":[{"id":999579,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999570\/revisions\/999579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999577"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}