{"id":1126820,"date":"2025-01-08T20:04:39","date_gmt":"2025-01-08T12:04:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126820.html"},"modified":"2025-01-08T20:04:42","modified_gmt":"2025-01-08T12:04:42","slug":"python%e5%a6%82%e4%bd%95%e6%8c%89%e9%a1%ba%e5%ba%8f%e5%8f%96%e5%87%ba%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126820.html","title":{"rendered":"python\u5982\u4f55\u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094024\/88cbaaa0-a006-4df8-a0f2-b0b7967b1671-1.webp\" alt=\"python\u5982\u4f55\u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python \u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u89e3\u6790\u6570\u7ec4\u3001\u6309\u987a\u5e8f\u53d6\u51fa\u6570\u7ec4\u5143\u7d20\u3001\u5904\u7406\u5f02\u5e38\u3002<\/strong> \u5176\u4e2d\uff0c\u89e3\u6790\u6570\u7ec4\u662f\u6700\u5173\u952e\u7684\u6b65\u9aa4\uff0c\u56e0\u4e3a\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\u683c\u5f0f\u53ef\u80fd\u5404\u4e0d\u76f8\u540c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u662f\u5904\u7406\u6587\u4ef6\u4e2d\u6570\u636e\u7684\u7b2c\u4e00\u6b65\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u5982 <code>open()<\/code> \u51fd\u6570\u3001<code>pandas<\/code> \u5e93\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u89e3\u6790\u6570\u7ec4\u662f\u6307\u5c06\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u8f6c\u6362\u4e3a Python \u7684\u5217\u8868\u5bf9\u8c61\u3002\u5e38\u89c1\u7684\u6570\u7ec4\u683c\u5f0f\u6709 JSON\u3001CSV \u7b49\u3002\u4ee5\u4e0b\u662f\u9488\u5bf9 JSON \u548c CSV \u683c\u5f0f\u7684\u89e3\u6790\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>1. JSON \u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>JSON \u683c\u5f0f\u7684\u6570\u7ec4\u901a\u5e38\u4f7f\u7528 <code>json<\/code> \u5e93\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def parse_json_array(content):<\/p>\n<p>    try:<\/p>\n<p>        data = json.loads(content)<\/p>\n<p>        if isinstance(data, list):<\/p>\n<p>            return data<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;The content is not a valid JSON array&quot;)<\/p>\n<p>    except json.JSONDecodeError:<\/p>\n<p>        raise ValueError(&quot;Failed to decode JSON&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. CSV \u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>CSV \u683c\u5f0f\u7684\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528 <code>csv<\/code> \u5e93\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def parse_csv_array(content):<\/p>\n<p>    data = []<\/p>\n<p>    reader = csv.reader(content.splitlines())<\/p>\n<p>    for row in reader:<\/p>\n<p>        data.append(row)<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6309\u987a\u5e8f\u53d6\u51fa\u6570\u7ec4\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u89e3\u6790\u51fa\u6570\u7ec4\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5176\u4ed6\u65b9\u6cd5\u6309\u987a\u5e8f\u53d6\u51fa\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_array_elements(array):<\/p>\n<p>    for element in array:<\/p>\n<p>        print(element)  # \u6216\u8005\u8fdb\u884c\u5176\u4ed6\u5904\u7406<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5f02\u5e38\u662f\u4fdd\u8bc1\u7a0b\u5e8f\u7a33\u5b9a\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5e38\u89c1\u7684\u5f02\u5e38\u5305\u62ec\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u683c\u5f0f\u9519\u8bef\u7b49\u3002\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u5f02\u5e38\u5904\u7406\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u4e3a\u5168\u9762\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main(file_path, format_type):<\/p>\n<p>    try:<\/p>\n<p>        content = read_file(file_path)<\/p>\n<p>        if format_type == &#39;json&#39;:<\/p>\n<p>            array = parse_json_array(content)<\/p>\n<p>        elif format_type == &#39;csv&#39;:<\/p>\n<p>            array = parse_csv_array(content)<\/p>\n<p>        else:<\/p>\n<p>            raise ValueError(&quot;Unsupported format type&quot;)<\/p>\n<p>        process_array_elements(array)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File not found: {file_path}&quot;)<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An unexpected error occurred: {e}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main(&quot;path_to_file&quot;, &quot;json&quot;)  # \u793a\u4f8b\u8c03\u7528<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a JSON \u6570\u7ec4\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">[<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684 Python \u4ee3\u7801\uff0c\u7528\u4e8e\u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def read_file(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<p>def parse_json_array(content):<\/p>\n<p>    try:<\/p>\n<p>        data = json.loads(content)<\/p>\n<p>        if isinstance(data, list):<\/p>\n<p>            return data<\/p>\n<p>        else:<\/p>\n<p>            raise ValueError(&quot;The content is not a valid JSON array&quot;)<\/p>\n<p>    except json.JSONDecodeError:<\/p>\n<p>        raise ValueError(&quot;Failed to decode JSON&quot;)<\/p>\n<p>def process_array_elements(array):<\/p>\n<p>    for element in array:<\/p>\n<p>        print(element)<\/p>\n<p>def main(file_path):<\/p>\n<p>    try:<\/p>\n<p>        content = read_file(file_path)<\/p>\n<p>        array = parse_json_array(content)<\/p>\n<p>        process_array_elements(array)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File not found: {file_path}&quot;)<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An unexpected error occurred: {e}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main(&quot;path_to_file.json&quot;)  # \u793a\u4f8b\u8c03\u7528<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u6837\u7684\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e JSON \u683c\u5f0f\u7684\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u6269\u5c55\u5230\u5176\u4ed6\u683c\u5f0f\uff0c\u5982 CSV\u3001XML \u7b49\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728 Python \u4e2d\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u4ef6\u5e76\u63d0\u53d6\u6570\u7ec4\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u6216\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u6bcf\u4e00\u884c\u7684\u6570\u7ec4\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u5229\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u7ec4\u5f62\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    arrays = [line.strip().split() for line in file]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u6587\u4ef6\u4e2d\u6bcf\u4e00\u884c\u7684\u6570\u7ec4\u6309\u987a\u5e8f\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n<p><strong>\u5982\u679c\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\u683c\u5f0f\u4e0d\u4e00\u81f4\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u9047\u5230\u6587\u4ef6\u4e2d\u6570\u7ec4\u683c\u5f0f\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u8fdb\u884c\u5224\u65ad\u548c\u5904\u7406\u3002\u53ef\u4ee5\u5728\u8bfb\u53d6\u6bcf\u884c\u4e4b\u524d\uff0c\u68c0\u67e5\u8be5\u884c\u662f\u5426\u7b26\u5408\u9884\u671f\u683c\u5f0f\uff0c\u786e\u4fdd\u53ea\u63d0\u53d6\u6709\u6548\u7684\u6570\u7ec4\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>try-except<\/code>\u7ed3\u6784\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u8df3\u8fc7\u90a3\u4e9b\u683c\u5f0f\u9519\u8bef\u7684\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u63d0\u53d6\u7684\u6570\u7ec4\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u6570\u7ec4\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>sort()<\/code>\u65b9\u6cd5\u6216<code>sorted()<\/code>\u51fd\u6570\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5217\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">for array in arrays:\n    array.sort()  # \u5bf9\u6bcf\u4e2a\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u63d0\u53d6\u51fa\u6765\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6309\u987a\u5e8f\u53d6\u51fa\u6587\u4ef6\u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u89e3\u6790\u6570\u7ec4\u3001\u6309\u987a\u5e8f\u53d6\u51fa\u6570\u7ec4\u5143\u7d20\u3001\u5904\u7406\u5f02\u5e38\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1126828,"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\/1126820"}],"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=1126820"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126820\/revisions"}],"predecessor-version":[{"id":1126830,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126820\/revisions\/1126830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126828"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}