{"id":1158313,"date":"2025-01-13T18:39:05","date_gmt":"2025-01-13T10:39:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1158313.html"},"modified":"2025-01-13T18:39:08","modified_gmt":"2025-01-13T10:39:08","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e6%96%87%e5%ad%97%e7%bc%96%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158313.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u6587\u5b57\u7f16\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200315\/736aca66-45a3-4fca-8315-c42944386044.webp\" alt=\"python\u5982\u4f55\u7ed9\u6587\u5b57\u7f16\u53f7\" \/><\/p>\n<p><p> <strong>Python\u7ed9\u6587\u5b57\u7f16\u53f7\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u3001enumerate\u51fd\u6570\u3001\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528for\u5faa\u73af\u548cenumerate\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e5f\u662f\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be6\u7ec6\u63cf\u8ff0enumerate\u65b9\u6cd5\u6765\u8fdb\u4e00\u6b65\u5c55\u5f00\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528enumerate\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ca\u5176\u5bf9\u5e94\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6587\u5b57\u8fdb\u884c\u7f16\u53f7\u3002<\/strong> \u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528enumerate\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u7d22\u5f15\u548c\u5143\u7d20\u3002\u5b83\u8fd4\u56de\u4e00\u4e2aenumerate\u5bf9\u8c61\uff0c\u53ef\u4ee5\u76f4\u63a5\u8f6c\u4e3a\u5217\u8868\u8fdb\u884c\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">texts = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>for index, text in enumerate(texts, start=1):<\/p>\n<p>    print(f&quot;{index}. {text}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate<\/code>\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9700\u8981\u904d\u5386\u7684\u5217\u8868\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684<code>start<\/code>\u53c2\u6570\uff0c\u6307\u5b9a\u7d22\u5f15\u4ece\u54ea\u4e2a\u6570\u5b57\u5f00\u59cb\uff0c\u9ed8\u8ba4\u4ece0\u5f00\u59cb\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ece1\u5f00\u59cb\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af<\/h2>\n<\/p>\n<p><p>for\u5faa\u73af\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\uff0c\u53ef\u4ee5\u624b\u52a8\u4e3a\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u4f7f\u7528<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">texts = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>index = 1<\/p>\n<p>for text in texts:<\/p>\n<p>    print(f&quot;{index}. {text}&quot;)<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u7ef4\u62a4\u4e00\u4e2a<code>index<\/code>\u53d8\u91cf\uff0c\u4ece1\u5f00\u59cb\uff0c\u6bcf\u904d\u5386\u4e00\u4e2a\u5143\u7d20\uff0c\u8be5\u53d8\u91cf\u589e\u52a01\uff0c\u4ece\u800c\u5b9e\u73b0\u4e3a\u6bcf\u4e2a\u5143\u7d20\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed9\u6587\u5b57\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">texts = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>numbered_texts = [f&quot;{i+1}. {text}&quot; for i, text in enumerate(texts)]<\/p>\n<p>print(numbered_texts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u4ee3\u7801\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u4e0a\u66f4\u4f18\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u6587\u5b57\u548c\u7f16\u53f7\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u4f7f\u7528<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">texts = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>numbered_dict = {}<\/p>\n<p>for index, text in enumerate(texts, start=1):<\/p>\n<p>    numbered_dict[text] = index<\/p>\n<p>print(numbered_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u4e3a\u6587\u5b57\uff0c\u503c\u4e3a\u5bf9\u5e94\u7684\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u53cd\u5411\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u53cd\u5411\u5b57\u5178\uff0c\u5373\u7f16\u53f7\u4e3a\u952e\uff0c\u6587\u5b57\u4e3a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">texts = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>numbered_dict = {index: text for index, text in enumerate(texts, start=1)}<\/p>\n<p>print(numbered_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u53cd\u5411\u5b57\u5178\uff0c\u65b9\u4fbf\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u867d\u7136\u5728\u7b80\u5355\u7684\u7f16\u53f7\u4efb\u52a1\u4e2d\u4e0d\u5e38\u7528\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u65f6\uff0c\u5b83\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6dfb\u52a0\u7f16\u53f7<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def add_numbering(text):<\/p>\n<p>    lines = text.split(&#39;\\n&#39;)<\/p>\n<p>    numbered_lines = [f&quot;{i+1}. {line}&quot; for i, line in enumerate(lines)]<\/p>\n<p>    return &#39;\\n&#39;.join(numbered_lines)<\/p>\n<p>text = &quot;apple\\nbanana\\ncherry&quot;<\/p>\n<p>numbered_text = add_numbering(text)<\/p>\n<p>print(numbered_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u5c06\u6587\u672c\u6309\u884c\u5206\u5272\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3a\u6bcf\u884c\u6dfb\u52a0\u7f16\u53f7\uff0c\u6700\u540e\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u5c06\u5176\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u66ff\u6362\u7f16\u53f7<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_numbering(text):<\/p>\n<p>    return re.sub(r&#39;^\\d+\\.\\s&#39;, &#39;&#39;, text, flags=re.MULTILINE)<\/p>\n<p>numbered_text = &quot;1. apple\\n2. banana\\n3. cherry&quot;<\/p>\n<p>replaced_text = replace_numbering(numbered_text)<\/p>\n<p>print(replaced_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>re.sub<\/code>\u65b9\u6cd5\uff0c\u5c06\u5339\u914d\u7684\u7f16\u53f7\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u53bb\u9664\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6309\u6bb5\u843d\u7f16\u53f7<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u6309\u6bb5\u843d\u7f16\u53f7\uff0c\u800c\u4e0d\u662f\u6309\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_paragraph_numbering(text):<\/p>\n<p>    paragraphs = text.split(&#39;\\n\\n&#39;)<\/p>\n<p>    numbered_paragraphs = [f&quot;Paragraph {i+1}:\\n{paragraph}&quot; for i, paragraph in enumerate(paragraphs)]<\/p>\n<p>    return &#39;\\n\\n&#39;.join(numbered_paragraphs)<\/p>\n<p>text = &quot;apple\\nbanana\\n\\ncherry\\ndate\\n\\nelderberry\\nfig&quot;<\/p>\n<p>numbered_text = add_paragraph_numbering(text)<\/p>\n<p>print(numbered_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6309\u53cc\u6362\u884c\u7b26\u5206\u5272\u6587\u672c\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u6bb5\u843d\u6dfb\u52a0\u7f16\u53f7\uff0c\u6700\u540e\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6309\u7ae0\u8282\u7f16\u53f7<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7ae0\u8282\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_chapter_numbering(text):<\/p>\n<p>    chapters = re.split(r&#39;\\n{3,}&#39;, text)<\/p>\n<p>    numbered_chapters = [f&quot;Chapter {i+1}:\\n{chapter}&quot; for i, chapter in enumerate(chapters)]<\/p>\n<p>    return &#39;\\n\\n\\n&#39;.join(numbered_chapters)<\/p>\n<p>text = &quot;apple\\nbanana\\n\\n\\ncherry\\ndate\\n\\n\\nelderberry\\nfig&quot;<\/p>\n<p>numbered_text = add_chapter_numbering(text)<\/p>\n<p>print(numbered_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6309\u4e09\u4e2a\u4ee5\u4e0a\u7684\u6362\u884c\u7b26\u5206\u5272\u6587\u672c\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7ae0\u8282\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7ed3\u5408GUI\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u4e2d\u5b9e\u73b0\u6587\u5b57\u7f16\u53f7\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Tkinter<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u901a\u5e38\u5df2\u7ecf\u5305\u542b\u5728Python\u5b89\u88c5\u5305\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efaTkinter\u5e94\u7528<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def add_numbering():<\/p>\n<p>    text = text_box.get(&quot;1.0&quot;, tk.END)<\/p>\n<p>    lines = text.split(&#39;\\n&#39;)<\/p>\n<p>    numbered_lines = [f&quot;{i+1}. {line}&quot; for i, line in enumerate(lines)]<\/p>\n<p>    numbered_text = &#39;\\n&#39;.join(numbered_lines)<\/p>\n<p>    text_box.delete(&quot;1.0&quot;, tk.END)<\/p>\n<p>    text_box.insert(tk.END, numbered_text)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Text Numbering&quot;)<\/p>\n<p>text_box = tk.Text(root, wrap=&#39;word&#39;, width=50, height=15)<\/p>\n<p>text_box.pack(padx=10, pady=10)<\/p>\n<p>number_button = tk.Button(root, text=&quot;Add Numbering&quot;, command=add_numbering)<\/p>\n<p>number_button.pack(pady=5)<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u5e94\u7528\uff0c\u5305\u542b\u4e00\u4e2a\u6587\u672c\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u70b9\u51fb\u6309\u94ae\u540e\uff0c\u5c06\u4e3a\u6587\u672c\u6846\u4e2d\u7684\u6bcf\u884c\u6587\u5b57\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u5927\u6587\u672c\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u6027\u80fd\u548c\u5185\u5b58\u5360\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u548c\u6587\u4ef6\u8fed\u4ee3\u5668\u6765\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u9010\u884c\u5904\u7406\u5927\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_numbering(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for index, line in enumerate(file, start=1):<\/p>\n<p>            yield f&quot;{index}. {line}&quot;<\/p>\n<p>file_path = &#39;large_text_file.txt&#39;<\/p>\n<p>with open(&#39;numbered_text_file.txt&#39;, &#39;w&#39;) as output_file:<\/p>\n<p>    for numbered_line in add_numbering(file_path):<\/p>\n<p>        output_file.write(numbered_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u7f16\u53f7\u540e\u5199\u5165\u65b0\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u6587\u4ef6\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u8fed\u4ee3\u5668\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u9002\u5408\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_numbering(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for index, line in enumerate(file, start=1):<\/p>\n<p>            yield f&quot;{index}. {line}&quot;<\/p>\n<p>file_path = &#39;large_text_file.txt&#39;<\/p>\n<p>output_path = &#39;numbered_text_file.txt&#39;<\/p>\n<p>with open(output_path, &#39;w&#39;) as output_file:<\/p>\n<p>    output_file.writelines(add_numbering(file_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6587\u4ef6\u8fed\u4ee3\u5668\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u7f16\u53f7\u540e\u5199\u5165\u65b0\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u591a\u79cd\u683c\u5f0f\u6587\u672c<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u79cd\u683c\u5f0f\u7684\u6587\u672c\uff0c\u5982Markdown\u3001HTML\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001Markdown\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5728Markdown\u6587\u672c\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4e3a\u6bcf\u4e2a\u6bb5\u843d\u6216\u6807\u9898\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_markdown_numbering(text):<\/p>\n<p>    lines = text.split(&#39;\\n&#39;)<\/p>\n<p>    numbered_lines = []<\/p>\n<p>    index = 1<\/p>\n<p>    for line in lines:<\/p>\n<p>        if line.startswith(&#39;#&#39;):<\/p>\n<p>            numbered_lines.append(f&quot;{index}. {line}&quot;)<\/p>\n<p>            index += 1<\/p>\n<p>        else:<\/p>\n<p>            numbered_lines.append(line)<\/p>\n<p>    return &#39;\\n&#39;.join(numbered_lines)<\/p>\n<p>text = &quot;# Header 1\\nContent\\n## Header 2\\nMore content&quot;<\/p>\n<p>numbered_text = add_markdown_numbering(text)<\/p>\n<p>print(numbered_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2aMarkdown\u6807\u9898\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>2\u3001HTML\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5728HTML\u6587\u672c\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4e3a\u6bcf\u4e2a\u6bb5\u843d\u6216\u6807\u9898\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>def add_html_numbering(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    headers = soup.find_all([&#39;h1&#39;, &#39;h2&#39;, &#39;h3&#39;, &#39;h4&#39;, &#39;h5&#39;, &#39;h6&#39;])<\/p>\n<p>    for index, header in enumerate(headers, start=1):<\/p>\n<p>        header.string = f&quot;{index}. {header.string}&quot;<\/p>\n<p>    return str(soup)<\/p>\n<p>html = &quot;&lt;h1&gt;Header 1&lt;\/h1&gt;&lt;p&gt;Content&lt;\/p&gt;&lt;h2&gt;Header 2&lt;\/h2&gt;&lt;p&gt;More content&lt;\/p&gt;&quot;<\/p>\n<p>numbered_html = add_html_numbering(html)<\/p>\n<p>print(numbered_html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6807\u9898\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u6269\u5c55\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6587\u5b57\u7f16\u53f7\u529f\u80fd\u6269\u5c55\u5230\u5176\u4ed6\u9886\u57df\uff0c\u5982\u6570\u636e\u5206\u6790\u3001\u81ea\u52a8\u5316\u62a5\u544a\u751f\u6210\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4e3a\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u884c\u6216\u6bcf\u5217\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [24, 27, 22]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.index = range(1, len(df) + 1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u5e93\u4e3a\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u884c\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u81ea\u52a8\u5316\u62a5\u544a\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u62a5\u544a\u751f\u6210\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4e3a\u62a5\u544a\u4e2d\u7684\u6bcf\u4e2a\u90e8\u5206\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>def add_numbering(doc):<\/p>\n<p>    for index, paragraph in enumerate(doc.paragraphs, start=1):<\/p>\n<p>        if paragraph.text.strip():<\/p>\n<p>            paragraph.text = f&quot;{index}. {paragraph.text}&quot;<\/p>\n<p>    return doc<\/p>\n<p>doc = Document()<\/p>\n<p>doc.add_paragraph(&quot;First paragraph&quot;)<\/p>\n<p>doc.add_paragraph(&quot;Second paragraph&quot;)<\/p>\n<p>numbered_doc = add_numbering(doc)<\/p>\n<p>numbered_doc.save(&quot;numbered_report.docx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528python-docx\u5e93\u4e3a\u62a5\u544a\u4e2d\u7684\u6bcf\u4e2a\u6bb5\u843d\u6dfb\u52a0\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u4e3a\u6587\u5b57\u6dfb\u52a0\u7f16\u53f7\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001enumerate\u51fd\u6570\u3001\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u5904\u7406\u5927\u6587\u4ef6\u548c\u591a\u79cd\u683c\u5f0f\u7684\u6587\u672c\uff0c\u6269\u5c55\u5230\u6570\u636e\u5206\u6790\u548c\u81ea\u52a8\u5316\u62a5\u544a\u751f\u6210\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<p><p><strong>Python\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u6587\u5b57\u7f16\u53f7\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u6dfb\u52a0\u7f16\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684split()\u65b9\u6cd5\u5c06\u6587\u672c\u5206\u5272\u4e3a\u5355\u4e2a\u5355\u8bcd\uff0c\u7136\u540e\u4f7f\u7528enumerate()\u51fd\u6570\u4e3a\u6bcf\u4e2a\u5355\u8bcd\u6dfb\u52a0\u7f16\u53f7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c&quot;\nnumbered_words = {index + 1: word for index, word in enumerate(text.split())}\nprint(numbered_words)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u7f16\u53f7\uff0c\u503c\u662f\u5355\u8bcd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4e3a\u6587\u672c\u4e2d\u7684\u884c\u6dfb\u52a0\u7f16\u53f7\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u4e3a\u6587\u672c\u4e2d\u7684\u6bcf\u4e00\u884c\u6dfb\u52a0\u7f16\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528readlines()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u6216\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u7ed3\u5408enumerate()\u51fd\u6570\u8fdb\u884c\u7f16\u53f7\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:\n    for index, line in enumerate(file.readlines(), start=1):\n        print(f&quot;{index}: {line.strip()}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6bcf\u4e00\u884c\u90fd\u4f1a\u88ab\u7f16\u53f7\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e0a\u7f16\u53f7\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u7f16\u53f7\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528enumerate()\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]\nfor index, value in enumerate(my_list, start=1):\n    print(f&quot;{index}: {value}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e3a\u5217\u8868\u5143\u7d20\u8fdb\u884c\u7f16\u53f7\u5e76\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed9\u6587\u5b57\u7f16\u53f7\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u3001enumerate\u51fd\u6570\u3001\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u6765 [&hellip;]","protected":false},"author":3,"featured_media":1158317,"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\/1158313"}],"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=1158313"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158313\/revisions"}],"predecessor-version":[{"id":1158319,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158313\/revisions\/1158319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158317"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}