{"id":1025283,"date":"2024-12-31T10:32:21","date_gmt":"2024-12-31T02:32:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025283.html"},"modified":"2024-12-31T10:32:24","modified_gmt":"2024-12-31T02:32:24","slug":"python%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%a4%9a%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025283.html","title":{"rendered":"python\u5982\u4f55\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e5b65cb8-06b6-4eaa-86da-91bf1b4c73ff.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u3001\u53cd\u659c\u6760\u3001\u4ee5\u53ca\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/strong> \u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u662f\u6700\u5e38\u89c1\u548c\u6700\u65b9\u4fbf\u7684\u65b9\u5f0f\u3002\u4e09\u91cd\u5f15\u53f7\u53ef\u4ee5\u8ba9\u5b57\u7b26\u4e32\u8de8\u8d8a\u591a\u884c\uff0c\u800c\u4fdd\u6301\u5176\u683c\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4e09\u91cd\u5f15\u53f7<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u91cd\u5f15\u53f7\u662fPython\u4e2d\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5355\u5f15\u53f7 (<code>&#39;&#39;&#39;<\/code>) \u6216\u4e09\u91cd\u53cc\u5f15\u53f7 (<code>&quot;&quot;&quot;<\/code>) \u6765\u5b9a\u4e49\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4e09\u91cd\u5f15\u53f7\u5141\u8bb8\u5b57\u7b26\u4e32\u8de8\u8d8a\u591a\u884c\uff0c\u5e76\u4e14\u4fdd\u6301\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u76f4\u89c2\uff0c\u800c\u4e14\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6362\u884c\u7b26\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\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 spans across multiple lines.<\/p>\n<p>Each line is separated by a newline character.&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>This is a multi-line string.<\/p>\n<p>It spans across multiple lines.<\/p>\n<p>Each line is separated by a newline character.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7<\/h3>\n<\/p>\n<p><p>\u4e09\u91cd\u5f15\u53f7\u662fPython\u4e2d\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u548c\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5355\u5f15\u53f7 (<code>&#39;&#39;&#39;<\/code>) \u6216\u4e09\u91cd\u53cc\u5f15\u53f7 (<code>&quot;&quot;&quot;<\/code>) \u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4e09\u91cd\u5f15\u53f7\u5141\u8bb8\u5b57\u7b26\u4e32\u8de8\u8d8a\u591a\u884c\uff0c\u5e76\u4e14\u4fdd\u6301\u5176\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\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 spans across multiple lines.<\/p>\n<p>Each line is separated by a newline character.&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u53cd\u659c\u6760<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760 (<code><\/code>) \u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53cd\u659c\u6760\u544a\u8bc9Python\u5b57\u7b26\u4e32\u5728\u4e0b\u4e00\u884c\u7ee7\u7eed\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4e0d\u5982\u4e09\u91cd\u5f15\u53f7\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;This is a multi-line string. \\<\/p>\n<p>It spans across multiple lines. \\<\/p>\n<p>Each line is separated by a newline character.&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u4f60\u53ef\u4ee5\u5c06\u6bcf\u4e00\u884c\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u5b83\u4eec\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;This is a multi-line string.&quot;,<\/p>\n<p>         &quot;It spans across multiple lines.&quot;,<\/p>\n<p>         &quot;Each line is separated by a newline character.&quot;]<\/p>\n<p>multi_line_string = &quot;\\n&quot;.join(lines)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>textwrap<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684 <code>textwrap<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>textwrap.dedent()<\/code> \u6765\u53bb\u9664\u591a\u884c\u5b57\u7b26\u4e32\u7684\u5171\u540c\u524d\u5bfc\u7a7a\u683c\uff0c\u6216\u8005\u4f7f\u7528 <code>textwrap.fill()<\/code> \u6765\u81ea\u52a8\u6362\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>multi_line_string = &quot;&quot;&quot;\\<\/p>\n<p>    This is a multi-line string.<\/p>\n<p>    It spans across multiple lines.<\/p>\n<p>    Each line is separated by a newline character.&quot;&quot;&quot;<\/p>\n<p>dedented_string = textwrap.dedent(multi_line_string)<\/p>\n<p>print(dedented_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u62ec\u53f7<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u5305\u542b\u5728\u4e00\u5bf9\u62ec\u53f7\u4e2d\uff0c\u8fd9\u6837Python\u4f1a\u81ea\u52a8\u5c06\u8fd9\u4e9b\u884c\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u800c\u4e0d\u9700\u8981\u663e\u5f0f\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = (<\/p>\n<p>    &quot;This is a multi-line string. &quot;<\/p>\n<p>    &quot;It spans across multiple lines. &quot;<\/p>\n<p>    &quot;Each line is separated by a newline character.&quot;<\/p>\n<p>)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 <code>+<\/code> \u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5e38\u7528\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>+<\/code> \u8fd0\u7b97\u7b26\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5728\u53ef\u8bfb\u6027\u4e0a\u4e0d\u5982\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;This is a multi-line string. &quot; + \\<\/p>\n<p>                    &quot;It spans across multiple lines. &quot; + \\<\/p>\n<p>                    &quot;Each line is separated by a newline character.&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 <code><\/code> \u548c <code>()<\/code> \u7ec4\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u53cd\u659c\u6760\u548c\u62ec\u53f7\u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u6574\u6d01\uff0c\u4f46\u4e5f\u53ef\u80fd\u964d\u4f4e\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = (&quot;This is a multi-line string. &quot;<\/p>\n<p>                     &quot;It spans across multiple lines. &quot;<\/p>\n<p>                     &quot;Each line is separated by a newline character.&quot;)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528 <code>textwrap<\/code> \u6a21\u5757\u7684 <code>wrap<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>dedent<\/code> \u548c <code>fill<\/code>\uff0c <code>textwrap<\/code> \u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86 <code>wrap<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u591a\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>long_string = &quot;This is a long string that we want to wrap into multiple lines.&quot;<\/p>\n<p>wrapped_list = textwrap.wrap(long_string, width=20)<\/p>\n<p>wrapped_string = &quot;\\n&quot;.join(wrapped_list)<\/p>\n<p>print(wrapped_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5d4c\u5165\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5d4c\u5165\u4ee3\u7801\u7247\u6bb5\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u52a0\u4e0a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">code_snippet = &quot;&quot;&quot;<\/p>\n<p>def hello_world():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>multi_line_string = f&quot;&quot;&quot;Here is a code snippet:<\/p>\n<p>{code_snippet}<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u7684HTML\u5185\u5bb9\u3002\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u662f\u4e00\u4e2a\u597d\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u4fdd\u6301HTML\u6807\u7b7e\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">html_content = &quot;&quot;&quot;<\/p>\n<p>&lt;div&gt;<\/p>\n<p>    &lt;p&gt;This is a paragraph.&lt;\/p&gt;<\/p>\n<p>    &lt;p&gt;This is another paragraph.&lt;\/p&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(html_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528<code>io.StringIO<\/code><\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u7684\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>io.StringIO<\/code>\u6765\u5904\u7406\u5b57\u7b26\u4e32\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<code>StringIO<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5f53\u4f5c\u6587\u4ef6\u5bf9\u8c61\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>output = io.StringIO()<\/p>\n<p>output.write(&quot;This is a multi-line string.\\n&quot;)<\/p>\n<p>output.write(&quot;It spans across multiple lines.\\n&quot;)<\/p>\n<p>output.write(&quot;Each line is separated by a newline character.\\n&quot;)<\/p>\n<p>multi_line_string = output.getvalue()<\/p>\n<p>output.close()<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5904\u7406\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>ConfigParser<\/code>\u6a21\u5757\u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from configparser import ConfigParser<\/p>\n<p>config = ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<p>multi_line_string = config.get(&#39;section&#39;, &#39;multi_line_string&#39;)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u884c\u6a21\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>multi_line_string = &quot;&quot;&quot;This is a multi-line string.<\/p>\n<p>It spans across multiple lines.<\/p>\n<p>Each line is separated by a newline character.&quot;&quot;&quot;<\/p>\n<p>pattern = re.compile(r&#39;^.*$&#39;, re.MULTILINE)<\/p>\n<p>matches = pattern.findall(multi_line_string)<\/p>\n<p>for match in matches:<\/p>\n<p>    print(match)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5904\u7406\u65e5\u5fd7\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u5c06\u65e5\u5fd7\u4fe1\u606f\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u8bfb\u53d6\u65e5\u5fd7\u65f6\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>logging.info(&quot;This is a multi-line string.\\nIt spans across multiple lines.\\nEach line is separated by a newline character.&quot;)<\/p>\n<p>with open(&#39;app.log&#39;, &#39;r&#39;) as file:<\/p>\n<p>    log_content = file.read()<\/p>\n<p>print(log_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5904\u7406CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.reader(csvfile, delimiter=&#39;,&#39;, quotechar=&#39;&quot;&#39;)<\/p>\n<p>    for row in reader:<\/p>\n<p>        multi_line_string = &#39;\\n&#39;.join(row)<\/p>\n<p>        print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5904\u7406JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06JSON\u6587\u4ef6\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as jsonfile:<\/p>\n<p>    data = json.load(jsonfile)<\/p>\n<p>    multi_line_string = data[&#39;multi_line_string&#39;]<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u5904\u7406XML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406XML\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u5c06XML\u6587\u4ef6\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>tree = ET.parse(&#39;data.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p>multi_line_string = root.find(&#39;multi_line_string&#39;).text<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u5904\u7406YAML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406YAML\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528<code>yaml<\/code>\u6a21\u5757\u5c06YAML\u6587\u4ef6\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>with open(&#39;data.yaml&#39;, &#39;r&#39;) as yamlfile:<\/p>\n<p>    data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(yamlfile)<\/p>\n<p>    multi_line_string = data[&#39;multi_line_string&#39;]<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT multi_line_string FROM strings WHERE id=1&#39;)<\/p>\n<p>multi_line_string = cursor.fetchone()[0]<\/p>\n<p>print(multi_line_string)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>requests<\/code>\u5e93\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com\/data&#39;)<\/p>\n<p>multi_line_string = response.text<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u662f\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\u548c\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\u3002\u4e0d\u540c\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u672c\u5904\u7406\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6587\u4ef6\u548c\u7f51\u7edc\u64cd\u4f5c\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5b57\u7b26\u4e32\u8de8\u8d8a\u591a\u884c\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;\u8fd9\u662f\u4e00\u4e2a\n\u591a\u884c\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u3002&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u7b26\u4e32\u5c06\u4fdd\u7559\u683c\u5f0f\u548c\u6362\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u53bb\u9664\u591a\u884c\u5b57\u7b26\u4e32\u7684\u524d\u5bfc\u548c\u5c3e\u968f\u7a7a\u767d\uff1f<\/strong><br \/>\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u591a\u884c\u5b57\u7b26\u4e32\u7684\u524d\u5bfc\u548c\u5c3e\u968f\u7a7a\u767d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff08\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\uff09\u4f1a\u88ab\u53bb\u9664\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;   \u8fd9\u662f\u4e00\u4e2a\n\u591a\u884c\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\u3002   &quot;&quot;&quot;\ncleaned_string = multiline_string.strip()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>cleaned_string<\/code>\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\u5c06\u88ab\u79fb\u9664\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>splitlines()<\/code>\u65b9\u6cd5\u5c06\u591a\u884c\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6839\u636e\u884c\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6bcf\u4e00\u884c\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\n\u8fd9\u662f\u7b2c\u4e8c\u884c\n\u8fd9\u662f\u7b2c\u4e09\u884c&quot;&quot;&quot;\nlines_list = multiline_string.splitlines()\n<\/code><\/pre>\n<p><code>lines_list<\/code>\u5c06\u5305\u542b\u4e09\u9879\uff0c\u5206\u522b\u5bf9\u5e94\u4e8e\u6bcf\u4e00\u884c\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u3001\u53cd\u659c\u6760\u3001\u4ee5\u53ca\u4f7f\u7528 join() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1025286,"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\/1025283"}],"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=1025283"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025283\/revisions"}],"predecessor-version":[{"id":1025287,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025283\/revisions\/1025287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025286"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}