{"id":1146790,"date":"2025-01-08T23:19:50","date_gmt":"2025-01-08T15:19:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146790.html"},"modified":"2025-01-08T23:19:53","modified_gmt":"2025-01-08T15:19:53","slug":"python-%e5%a6%82%e4%bd%95%e8%a1%a8%e8%be%be%e6%af%8f%e4%b8%80%e8%a1%8c%e7%9a%84%e7%ac%ac%e4%b8%80%e4%b8%aa%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146790.html","title":{"rendered":"python \u5982\u4f55\u8868\u8fbe\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182659\/60ace175-8acd-4ab5-8cc7-a94738d5d17c.webp\" alt=\"python \u5982\u4f55\u8868\u8fbe\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python \u8868\u8fbe\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528\u5217\u8868\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u4f7f\u7528pandas\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u662f\u6700\u57fa\u7840\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u6216\u6570\u636e\u6e90\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.readlines()<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = [line.split()[0] for line in data]<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u6bcf\u4e00\u884c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u56e0\u4e3a\u751f\u6210\u5668\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def first_elements_generator(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield line.split()[0]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = list(first_elements_generator(&#39;data.txt&#39;))<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 pandas \u5e93<\/h3>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u4f7f\u7528 pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u6587\u4ef6\u5e76\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a DataFrame<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.txt&#39;, header=None)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = df[0].tolist()<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 pandas \u8bfb\u53d6\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u8f6c\u6362\u4e3a DataFrame\u3002\u7136\u540e\uff0c\u6211\u4eec\u63d0\u53d6 DataFrame \u7684\u7b2c\u4e00\u5217\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 pandas \u8bfb\u53d6\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a DataFrame<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.txt&#39;, header=None)<\/p>\n<p>def first_elements_generator(df):<\/p>\n<p>    for index, row in df.iterrows():<\/p>\n<p>        yield row[0]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = list(first_elements_generator(df))<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7ed3\u5408\u4e86 pandas \u7684\u5f3a\u5927\u6570\u636e\u5904\u7406\u80fd\u529b\u548c\u751f\u6210\u5668\u7684\u9ad8\u6548\u6027\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u6587\u4ef6\u53ef\u80fd\u5177\u6709\u7279\u5b9a\u7684\u683c\u5f0f\uff0c\u4f8b\u5982CSV\u3001JSON\u3001Excel\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u548c\u65b9\u6cd5\u8bfb\u53d6\u8fd9\u4e9b\u683c\u5f0f\u7684\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>CSV \u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 csv \u6a21\u5757\u6216 pandas \u5e93\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u4f7f\u7528 csv \u6a21\u5757\u8bfb\u53d6 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    first_elements = [row[0] for row in reader]<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528 pandas \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u8bfb\u53d6 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = df.iloc[:, 0].tolist()<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>JSON \u6587\u4ef6\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 json \u6a21\u5757\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6 JSON \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = [item[0] for item in data]<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Excel \u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u529e\u516c\u6587\u6863\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 pandas \u5e93\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u8bfb\u53d6 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = df.iloc[:, 0].tolist()<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u9700\u8981\u5904\u7406\u975e\u5e38\u5927\u89c4\u6a21\u7684\u6570\u636e\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6\u5927\u89c4\u6a21 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>chunk_size = 1000<\/p>\n<p>first_elements = []<\/p>\n<p>for chunk in pd.read_csv(&#39;large_data.csv&#39;, chunksize=chunk_size):<\/p>\n<p>    first_elements.extend(chunk.iloc[:, 0].tolist())<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 pandas \u7684 <code>read_csv<\/code> \u51fd\u6570\u5206\u5757\u8bfb\u53d6 CSV \u6587\u4ef6\uff0c\u6bcf\u6b21\u8bfb\u53d6 1000 \u884c\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u800c\u4e0d\u4f1a\u5360\u7528\u8fc7\u591a\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Python \u7684 <code>threading<\/code> \u6216 <code>multiprocessing<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\u5e76\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def process_chunk(chunk, results):<\/p>\n<p>    results.extend(chunk.iloc[:, 0].tolist())<\/p>\n<p>chunk_size = 1000<\/p>\n<p>first_elements = []<\/p>\n<p>results = []<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6\u5927\u89c4\u6a21 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>for chunk in pd.read_csv(&#39;large_data.csv&#39;, chunksize=chunk_size):<\/p>\n<p>    thread = threading.Thread(target=process_chunk, args=(chunk, results))<\/p>\n<p>    thread.start()<\/p>\n<p>    thread.join()<\/p>\n<p>first_elements.extend(results)<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    return chunk.iloc[:, 0].tolist()<\/p>\n<p>chunk_size = 1000<\/p>\n<p>first_elements = []<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u8fdb\u7a0b\u6c60\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>with multiprocessing.Pool() as pool:<\/p>\n<p>    results = pool.map(process_chunk, pd.read_csv(&#39;large_data.csv&#39;, chunksize=chunk_size))<\/p>\n<h2><strong>\u5408\u5e76\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    first_elements.extend(result)<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u636e\u53ef\u80fd\u662f\u5d4c\u5957\u7684\uff0c\u4f8b\u5982 JSON \u6587\u4ef6\u4e2d\u7684\u5d4c\u5957\u5217\u8868\u6216\u5b57\u5178\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def extract_first_elements(data):<\/p>\n<p>    if isinstance(data, list):<\/p>\n<p>        return [item[0] for item in data]<\/p>\n<p>    elif isinstance(data, dict):<\/p>\n<p>        return [extract_first_elements(value) for key, value in data.items()]<\/p>\n<h2><strong>\u8bfb\u53d6 JSON \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;nested_data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = extract_first_elements(data)<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u6570\u636e\u5e93\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 SQL \u67e5\u8be2\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230 SQLite \u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;data.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c SQL \u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT column1 FROM table_name&quot;)<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = [row[0] for row in cursor.fetchall()]<\/p>\n<p>print(first_elements)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406API\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u6765\u81eaAPI\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 requests \u6a21\u5757\u83b7\u53d6\u6570\u636e\u5e76\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u83b7\u53d6 API \u6570\u636e<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p>data = response.json()<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e<\/strong><\/h2>\n<p>first_elements = [item[0] for item in data]<\/p>\n<p>print(first_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5728 Python \u4e2d\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001\u751f\u6210\u5668\u3001pandas \u5e93\uff0c\u4ee5\u53ca\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\uff08\u5982 CSV\u3001JSON\u3001Excel \u6587\u4ef6\uff09\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5206\u5757\u8bfb\u53d6\u3001\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\uff0c\u4ee5\u53ca\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3001\u6570\u636e\u5e93\u6570\u636e\u548cAPI\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e0b\u9ad8\u6548\u5730\u5904\u7406\u548c\u63d0\u53d6\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6587\u672c\u6587\u4ef6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7\u5206\u5272\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_file.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        first_data = line.split()[0]  # \u4f7f\u7528split()\u65b9\u6cd5\u5206\u5272\u884c\u5e76\u63d0\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\n        print(first_data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u6587\u4ef6\u3002\u5982\u679c\u6570\u636e\u662f\u4ee5\u5176\u4ed6\u5b57\u7b26\u5206\u9694\u7684\uff08\u5982\u9017\u53f7\uff09\uff0c\u53ef\u4ee5\u5728<code>split()<\/code>\u4e2d\u6307\u5b9a\u5206\u9694\u7b26\u3002<\/p>\n<p><strong>\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u6b63\u5728\u5904\u7406CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\uff0c\u65b9\u4fbf\u5730\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nwith open(&#39;your_file.csv&#39;, &#39;r&#39;) as csvfile:\n    reader = csv.reader(csvfile)\n    for row in reader:\n        print(row[0])  # row[0]\u5373\u4e3a\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u60a8\u80fd\u591f\u6b63\u786e\u5904\u7406\u5305\u542b\u9017\u53f7\u7684\u6587\u672c\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\uff1f<\/strong><br \/>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u9009\u62e9\u3002\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u6570\u636e\u5e76\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>pandas<\/code>\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.read_csv(&#39;your_file.csv&#39;)  # \u8bfb\u53d6CSV\u6587\u4ef6\nfirst_data = df.iloc[:, 0]  # \u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\nprint(first_data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u5e76\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u6781\u5927\u5730\u63d0\u5347\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8868\u8fbe\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528\u5217\u8868\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u4f7f\u7528pandas\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1146796,"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\/1146790"}],"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=1146790"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146790\/revisions"}],"predecessor-version":[{"id":1146798,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146790\/revisions\/1146798"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146796"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}