{"id":1127607,"date":"2025-01-08T20:11:51","date_gmt":"2025-01-08T12:11:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127607.html"},"modified":"2025-01-08T20:11:54","modified_gmt":"2025-01-08T12:11:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96txt%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%ad%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127607.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094551\/52cd6fd4-aeef-49b5-b002-87679c06a017.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5982\u4f55\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u6570\u5b57<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u3001\u4f7f\u7528open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u7684\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002Python\u7684os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<h2><strong>\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>for filename in os.listdir(folder_path):<\/p>\n<p>    if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, filename)<\/p>\n<p>        print(f&#39;Reading file: {file_path}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u4ee5.txt\u7ed3\u5c3e\u7684\u6587\u4ef6\uff0c\u5e76\u6253\u5370\u51fa\u5b83\u4eec\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u904d\u5386\u5230\u6bcf\u4e2a\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u9700\u8981\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002Python\u7684open\u51fd\u6570\u4f7f\u5f97\u8fd9\u4e00\u8fc7\u7a0b\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>for filename in os.listdir(folder_path):<\/p>\n<p>    if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, filename)<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(f&#39;Content of {filename}:&#39;)<\/p>\n<p>            print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u5e76\u6253\u5370\u51fa\u6bcf\u4e2atxt\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u9700\u8981\u4ece\u4e2d\u63d0\u53d6\u51fa\u6570\u5b57\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import re<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>for filename in os.listdir(folder_path):<\/p>\n<p>    if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, filename)<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/p>\n<p>            numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>            print(f&#39;Numbers in {filename}: {numbers}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u6bcf\u4e2atxt\u6587\u4ef6\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u7684\u6570\u5b57\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u5b9e\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u548c\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u5c06\u63d0\u53d6\u7684\u6570\u5b57\u4fdd\u5b58\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6216\u8005\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4fdd\u5b58\u63d0\u53d6\u7684\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u63d0\u53d6\u7684\u6570\u5b57\u4fdd\u5b58\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import re<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>all_numbers = []<\/p>\n<p>for filename in os.listdir(folder_path):<\/p>\n<p>    if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, filename)<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>            all_numbers.extend(numbers)<\/p>\n<p>print(f&#39;All extracted numbers: {all_numbers}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u6587\u4ef6\u4e2d\u63d0\u53d6\u7684\u6570\u5b57\u4fdd\u5b58\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0ctxt\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5bb9\u3002\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u6761\u4ef6\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import re<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>all_numbers = []<\/p>\n<p>for filename in os.listdir(folder_path):<\/p>\n<p>    if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, filename)<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            # \u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>            if &#39;TypeA&#39; in content:<\/p>\n<p>                numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>                all_numbers.extend(numbers)<\/p>\n<p>            elif &#39;TypeB&#39; in content:<\/p>\n<p>                numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>                all_numbers.extend(numbers)<\/p>\n<p>print(f&#39;All extracted numbers: {all_numbers}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6587\u4ef6\u5185\u5bb9\u7684\u4e0d\u540c\u7c7b\u578b\uff0c\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u3001\u4f7f\u7528open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u7684\u591a\u4e2atxt\u6587\u4ef6\uff0c\u5e76\u63d0\u53d6\u51fa\u5176\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u4e3a\u4f60\u7684Python\u7f16\u7a0b\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\u548c\u601d\u8def\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6txt\u6587\u4ef6\u4e2d\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6txt\u6587\u4ef6\u3002\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u7c7b\u578b\u8f6c\u6362\u5c06\u6587\u4ef6\u4e2d\u7684\u6570\u5b57\u63d0\u53d6\u5e76\u5b58\u50a8\u4e3a\u5217\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\u548c\u7a7a\u683c\uff0c\u518d\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002<\/p>\n<p><strong>\u8bfb\u53d6txt\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u683c\u5f0f\u9519\u8bef\u6216\u6743\u9650\u95ee\u9898\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002\u901a\u8fc7\u63d0\u4f9b\u9002\u5f53\u7684\u9519\u8bef\u6d88\u606f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u7406\u89e3\u95ee\u9898\u6240\u5728\u3002<\/p>\n<p><strong>\u5728\u8bfb\u53d6txt\u6587\u4ef6\u540e\uff0c\u5982\u4f55\u5bf9\u63d0\u53d6\u7684\u6570\u5b57\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\uff1f<\/strong><br \/>\u4e00\u65e6\u4ecetxt\u6587\u4ef6\u4e2d\u6210\u529f\u8bfb\u53d6\u5230\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u6570\u5b66\u51fd\u6570\u6216\u7b2c\u4e09\u65b9\u5e93\uff08\u5982NumPy\u6216Pandas\uff09\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002\u53ef\u4ee5\u6267\u884c\u7edf\u8ba1\u5206\u6790\uff0c\u5982\u8ba1\u7b97\u5e73\u5747\u503c\u3001\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\uff0c\u6216\u8005\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\uff0c\u4ee5\u5e2e\u52a9\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5982\u4f55\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u6570\u5b57 \u5728Python\u4e2d\uff0c\u8bfb\u53d6txt\u6587\u4ef6\u5939\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u6765\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":1127614,"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\/1127607"}],"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=1127607"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127607\/revisions"}],"predecessor-version":[{"id":1127616,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127607\/revisions\/1127616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127614"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}