{"id":1019160,"date":"2024-12-27T12:52:02","date_gmt":"2024-12-27T04:52:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019160.html"},"modified":"2024-12-27T12:52:29","modified_gmt":"2024-12-27T04:52:29","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e8%a1%8c%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019160.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u884c\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161721\/ee4c3ede-734d-4bc4-b3f0-19fb1362beb6.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u884c\u6570\" \/><\/p>\n<p><p> \u8981\u83b7\u53d6Python\u4e2d\u6587\u4ef6\u7684\u884c\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u5305\u62ec\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>readlines()<\/code>\u3001\u5229\u7528<code>enumerate()<\/code>\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u662f\u4e00\u79cd\u5185\u5b58\u53cb\u597d\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u5927\u6587\u4ef6\u3002\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u53ef\u4ee5\u4f7f\u7528<code>with open<\/code>\u8bed\u53e5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u6bcf\u8bfb\u53d6\u4e00\u884c\uff0c\u8ba1\u6570\u5668\u52a0\u4e00\uff0c\u8fd9\u6837\u5728\u4e0d\u5c06\u6587\u4ef6\u5185\u5bb9\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\uff0c\u4e5f\u80fd\u7edf\u8ba1\u6587\u4ef6\u7684\u884c\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u5927\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u5c06\u6574\u4e2a\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\u3002\u6b64\u5916\uff0c<code>with open<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u83b7\u53d6\u6587\u4ef6\u884c\u6570\u7684\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u662f\u83b7\u53d6\u6587\u4ef6\u884c\u6570\u7684\u4e00\u79cd\u9ad8\u6548\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u3002\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u8f7d\u5165\u5185\u5b58\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename):<\/p>\n<p>    count = 0<\/p>\n<p>    with open(filename, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            count += 1<\/p>\n<p>    return count<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u6bcf\u8bfb\u53d6\u4e00\u884c\uff0c\u8ba1\u6570\u5668<code>count<\/code>\u52a0\u4e00\u3002<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>readlines()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u6587\u4ef6\u7684\u6240\u6709\u884c\u8bfb\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6bcf\u4e00\u884c\u4f5c\u4e3a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u5217\u8868\u957f\u5ea6\u6765\u8ba1\u7b97\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines_with_readlines(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>    return len(lines)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines_with_readlines(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>readlines()<\/code>\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5b83\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5904\u7406\u975e\u5e38\u5927\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>enumerate()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\u65f6\u63d0\u4f9b\u7d22\u5f15\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u884c\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines_with_enumerate(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        for count, line in enumerate(file, 1):<\/p>\n<p>            pass<\/p>\n<p>    return count<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines_with_enumerate(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c<code>enumerate()<\/code>\u4ece1\u5f00\u59cb\u8ba1\u6570\u3002\u6211\u4eec\u5728\u904d\u5386\u6587\u4ef6\u65f6\uff0c\u4e0d\u65ad\u66f4\u65b0\u884c\u53f7\uff0c\u6700\u540e\u7684\u884c\u53f7\u5c31\u662f\u6587\u4ef6\u7684\u603b\u884c\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u540c\u6837\u907f\u514d\u4e86\u5c06\u6574\u4e2a\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>count<\/code>\u65b9\u6cd5\u7ed3\u5408<code>read<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>count<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba1\u6362\u884c\u7b26\u7684\u6570\u91cf\uff0c\u4ece\u800c\u8ba1\u7b97\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines_with_read(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    return content.count(&#39;\\n&#39;) + 1  # \u52a01\u662f\u56e0\u4e3a\u6700\u540e\u4e00\u884c\u53ef\u80fd\u6ca1\u6709\u6362\u884c\u7b26<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines_with_read(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u540c\u6837\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u975e\u5e38\u5927\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5229\u7528<code>Path<\/code>\u5bf9\u8c61\u7684<code>read_text<\/code>\u65b9\u6cd5\uff08Python 3.4+\uff09<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8ePython 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path<\/code>\u5bf9\u8c61\u3002<code>read_text<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u7edf\u8ba1\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def count_lines_with_pathlib(filename):<\/p>\n<p>    content = Path(filename).read_text(encoding=&#39;utf-8&#39;)<\/p>\n<p>    return content.count(&#39;\\n&#39;) + 1<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines_with_pathlib(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>Path<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u53ca\u6587\u4ef6\u64cd\u4f5c\uff0c\u4f46\u5728\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u65f6\u4ecd\u7136\u9700\u8981\u6ce8\u610f\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u5916\u90e8\u5de5\u5177\uff08\u5982<code>wc<\/code>\uff09<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u8ba1\u7b97\u6587\u4ef6\u884c\u6570\u3002\u4f8b\u5982\uff0c\u5728Unix\/Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>wc<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def count_lines_with_wc(filename):<\/p>\n<p>    result = subprocess.run([&#39;wc&#39;, &#39;-l&#39;, filename], stdout=subprocess.PIPE)<\/p>\n<p>    return int(result.stdout.split()[0])<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>print(f&quot;The file {filename} has {count_lines_with_wc(filename)} lines.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u547d\u4ee4\uff0c\u56e0\u6b64\u53ea\u80fd\u5728\u652f\u6301\u8fd9\u4e9b\u547d\u4ee4\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\u3002\u4e0d\u8fc7\uff0c\u5b83\u901a\u5e38\u6bd4Python\u539f\u751f\u65b9\u6cd5\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u884c\u6570\uff0c\u4e3b\u8981\u53d6\u51b3\u4e8e\u6587\u4ef6\u7684\u5927\u5c0f\u4ee5\u53ca\u7a0b\u5e8f\u7684\u6027\u80fd\u8981\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5185\u5b58\u4f7f\u7528<\/strong>\uff1a\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u548c<code>enumerate()<\/code>\u65b9\u6cd5\u66f4\u5408\u9002\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u4fbf\u6027<\/strong>\uff1a\u5bf9\u4e8e\u5c0f\u6587\u4ef6\u6216\u8005\u4e0d\u5173\u5fc3\u5185\u5b58\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>readlines()<\/code>\u6216<code>read<\/code>\u65b9\u6cd5\u4f1a\u66f4\u52a0\u7b80\u4fbf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u6027<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u5c3d\u91cf\u4f7f\u7528Python\u5185\u7f6e\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u5916\u90e8\u547d\u4ee4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1a\u5728Linux\/Unix\u7cfb\u7edf\u4e0a\uff0c<code>wc<\/code>\u547d\u4ee4\u901a\u5e38\u6bd4Python\u5b9e\u73b0\u7684\u65b9\u5f0f\u66f4\u5feb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u884c\u6570\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u7a7a\u6587\u4ef6\u3001\u6ca1\u6709\u6362\u884c\u7b26\u7684\u6587\u4ef6\u7ed3\u5c3e\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7a7a\u6587\u4ef6<\/strong>\uff1a\u6240\u6709\u65b9\u6cd5\u90fd\u80fd\u6b63\u786e\u5904\u7406\u7a7a\u6587\u4ef6\uff0c\u8fd4\u56de\u7684\u884c\u6570\u4e3a0\u3002<\/li>\n<li><strong>\u6700\u540e\u4e00\u884c\u6ca1\u6709\u6362\u884c\u7b26<\/strong>\uff1a\u5bf9\u4e8e<code>read<\/code>\u548c<code>count<\/code>\u65b9\u6cd5\uff0c\u9700\u8981\u5728\u8ba1\u7b97\u7ed3\u679c\u65f6\u52a01\uff0c\u4ee5\u786e\u4fdd\u6ca1\u6709\u9057\u6f0f\u6700\u540e\u4e00\u884c\u3002<\/li>\n<\/ul>\n<p><p>\u4e5d\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u6587\u4ef6\u884c\u6570\u7684\u529f\u80fd\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u90fd\u6709\u5e94\u7528\uff0c\u5305\u62ec\u65e5\u5fd7\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u7b49\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u884c\u6570\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u8fd8\u80fd\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u65e0\u8bba\u662f\u9010\u884c\u8bfb\u53d6\u3001\u4f7f\u7528<code>readlines()<\/code>\u3001<code>enumerate()<\/code>\uff0c\u8fd8\u662f\u8c03\u7528\u5916\u90e8\u547d\u4ee4\uff0c\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u3002\u4e86\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u5c06\u6709\u52a9\u4e8e\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5feb\u901f\u8bfb\u53d6\u5927\u578b\u6587\u4ef6\u7684\u884c\u6570\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>sum(1 for line in open(&#39;filename.txt&#39;))<\/code>\u7684\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u8bfb\u53d6\u884c\u6570\u3002\u6b64\u65b9\u6cd5\u4f1a\u9010\u884c\u8fed\u4ee3\u6587\u4ef6\uff0c\u5185\u5b58\u5360\u7528\u8f83\u4f4e\uff0c\u9002\u5408\u5927\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u5173\u95ed\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u5173\u95ed\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u83b7\u53d6\u6587\u4ef6\u884c\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86\u5185\u7f6e\u7684\u65b9\u6cd5\u5916\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u901a\u8fc7<code>pandas.read_csv(&#39;filename.txt&#39;).shape[0]<\/code>\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u884c\u6570\uff0c\u524d\u63d0\u662f\u6587\u4ef6\u683c\u5f0f\u9002\u5408<code>pandas<\/code>\u8bfb\u53d6\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u65f6\u5c24\u5176\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728\u83b7\u53d6\u884c\u6570\u65f6\uff0c\u5982\u4f55\u5ffd\u7565\u7a7a\u884c\u6216\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u52a0\u5165\u6761\u4ef6\u68c0\u67e5\u6765\u8fc7\u6ee4\u7a7a\u884c\u6216\u7279\u5b9a\u884c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;) as f:\n    line_count = sum(1 for line in f if line.strip())  # \u53ea\u8ba1\u6570\u975e\u7a7a\u884c\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u7edf\u8ba1\u7684\u884c\u6570\u4ec5\u5305\u542b\u5b9e\u9645\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u884c\u6570\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u53d6Python\u4e2d\u6587\u4ef6\u7684\u884c\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570readlines()\u3001\u5229\u7528e [&hellip;]","protected":false},"author":3,"featured_media":1019190,"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\/1019160"}],"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=1019160"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019160\/revisions"}],"predecessor-version":[{"id":1019192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019160\/revisions\/1019192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019190"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}