{"id":1076673,"date":"2025-01-08T11:55:27","date_gmt":"2025-01-08T03:55:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076673.html"},"modified":"2025-01-08T11:55:29","modified_gmt":"2025-01-08T03:55:29","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%af%bb%e5%8f%96%e5%8d%81%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076673.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u540c\u65f6\u8bfb\u53d6\u5341\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181132\/c44c4706-d229-4e46-9664-0df94ac130dc.webp\" alt=\"python\u4e2d\u5982\u4f55\u540c\u65f6\u8bfb\u53d6\u5341\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u540c\u65f6\u8bfb\u53d6\u5341\u884c\u5185\u5bb9\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001\u6587\u4ef6\u7684<code>readlines<\/code>\u65b9\u6cd5\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u3001<code>readlines()<\/code>\u65b9\u6cd5\u3001<code>islice<\/code>\u51fd\u6570\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>itertools.islice<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6309\u9700\u63d0\u53d6\u7279\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528 <code>itertools.islice<\/code> \u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u5341\u884c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 <code>itertools.islice<\/code> \u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u5341\u884c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>def read_first_ten_lines(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        first_ten_lines = list(itertools.islice(file, 10))<\/p>\n<p>    return first_ten_lines<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>lines = read_first_ten_lines(file_path)<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528 <code>with open<\/code> \u7ed3\u6784\u8bfb\u53d6\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>with open<\/code> \u7ed3\u6784\u8bfb\u53d6\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u5b89\u5168\u7684\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528 <code>close()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_with_with_open(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    return content<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>content = read_file_with_with_open(file_path)<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><code>for<\/code> \u5faa\u73af\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u53ef\u4ee5\u7ed3\u5408 <code>enumerate<\/code> \u51fd\u6570\u6765\u63a7\u5236\u8bfb\u53d6\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_with_for_loop(file_path, num_lines=10):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for i, line in enumerate(file):<\/p>\n<p>            if i &lt; num_lines:<\/p>\n<p>                print(line, end=&#39;&#39;)<\/p>\n<p>            else:<\/p>\n<p>                break<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>read_file_with_for_loop(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528 <code>readlines()<\/code> \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c<\/strong><\/p>\n<\/p>\n<p><p><code>readlines()<\/code> \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u524d\u5341\u884c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_with_readlines(file_path, num_lines=10):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>    return lines[:num_lines]<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>lines = read_file_with_readlines(file_path)<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c <code>islice<\/code> \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u7ed3\u5408 <code>islice<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u8bfb\u53d6\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>def read_file_with_generator(file_path, num_lines=10):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        lines = (line for line in file)<\/p>\n<p>        first_ten_lines = list(itertools.islice(lines, num_lines))<\/p>\n<p>    return first_ten_lines<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>lines = read_file_with_generator(file_path)<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u4f7f\u7528 <code>pandas<\/code> \u5e93\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u5341\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u662f\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982 CSV \u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u5e93\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u5341\u884c\u3002<code>pandas<\/code> \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def read_csv_with_pandas(file_path, num_lines=10):<\/p>\n<p>    df = pd.read_csv(file_path, nrows=num_lines)<\/p>\n<p>    return df<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>file_path = &#39;example.csv&#39;<\/p>\n<p>df = read_csv_with_pandas(file_path)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u5341\u884c\u5185\u5bb9\u3002\u6839\u636e\u6587\u4ef6\u7684\u7c7b\u578b\u548c\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u3002\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528 <code>itertools.islice<\/code> \u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u9ad8\u6548\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6bd5\u540e\u6b63\u786e\u5173\u95ed\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u5341\u884c\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\u7ed3\u5408<code>readlines()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u524d\u5341\u884c\u3002\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">files = [&#39;file1.txt&#39;, &#39;file2.txt&#39;]  # \u6587\u4ef6\u540d\u5217\u8868\nlines = {}\n\nfor file in files:\n    with open(file, &#39;r&#39;) as f:\n        lines[file] = f.readlines()[:10]  # \u8bfb\u53d6\u524d\u5341\u884c\n\nprint(lines)  # \u6253\u5370\u6bcf\u4e2a\u6587\u4ef6\u7684\u524d\u5341\u884c\n<\/code><\/pre>\n<p><strong>\u5728\u8bfb\u53d6\u5341\u884c\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e2d\u884c\u6570\u4e0d\u8db3\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u6587\u4ef6\u7684\u884c\u6570\u5c11\u4e8e\u5341\u884c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7684\u884c\u6570\u6765\u907f\u514d\u7d22\u5f15\u9519\u8bef\u3002\u53ef\u4ee5\u68c0\u67e5\u8bfb\u53d6\u7684\u884c\u6570\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\uff0c\u6bd4\u5982\u586b\u5145\u7a7a\u884c\u6216\u8bb0\u5f55\u63d0\u793a\u4fe1\u606f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as f:\n    lines = f.readlines()\n\nif len(lines) &lt; 10:\n    print(&quot;\u6587\u4ef6\u884c\u6570\u4e0d\u8db3\u5341\u884c\uff0c\u5b9e\u9645\u884c\u6570\u4e3a:&quot;, len(lines))\nelse:\n    lines = lines[:10]  # \u8bfb\u53d6\u524d\u5341\u884c\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u8bfb\u53d6\u5341\u884c\u6570\u636e\u7684\u6548\u7387\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>readlines()<\/code>\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u884c\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>itertools.islice()<\/code>\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u63d0\u53d6\u6240\u9700\u7684\u884c\u6570\uff0c\u907f\u514d\u5185\u5b58\u8d1f\u62c5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from itertools import islice\n\nwith open(&#39;large_file.txt&#39;, &#39;r&#39;) as f:\n    first_ten_lines = list(islice(f, 10))  # \u53ea\u8bfb\u53d6\u524d\u5341\u884c\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u540c\u65f6\u8bfb\u53d6\u5341\u884c\u5185\u5bb9\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001\u6587\u4ef6\u7684readlines\u65b9\u6cd5\u7b49\u3002\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":1076679,"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\/1076673"}],"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=1076673"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076673\/revisions"}],"predecessor-version":[{"id":1076682,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076673\/revisions\/1076682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}