{"id":1057919,"date":"2024-12-31T15:12:00","date_gmt":"2024-12-31T07:12:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057919.html"},"modified":"2024-12-31T15:12:02","modified_gmt":"2024-12-31T07:12:02","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e9%9b%86%e8%bf%9b%e8%a1%8c%e5%af%bc%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1057919.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u636e\u96c6\u8fdb\u884c\u5bfc\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a0a12869-adda-40aa-a5f0-ac9bdf615065.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u636e\u96c6\u8fdb\u884c\u5bfc\u5165\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u96c6\u5bfc\u5165\uff0c\u5305\u62ec\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3001\u4f7f\u7528numpy\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3001\u4f7f\u7528SQLAlchemy\u4ece\u6570\u636e\u5e93\u5bfc\u5165\u6570\u636e\u3001\u4f7f\u7528xlrd\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3apandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u548c\u7075\u6d3b\u7684\u8bfb\u53d6\u65b9\u5f0f\u3002\u901a\u8fc7<code>pd.read_csv()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5bfc\u5165\u5230DataFrame\u4e2d\uff0c\u5e76\u8fdb\u884c\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PANDAS\u8bfb\u53d6CSV\u6587\u4ef6<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5c24\u5176\u64c5\u957f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u8bfb\u53d6CSV\u6587\u4ef6\u662fpandas\u6700\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\uff0c\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5bfc\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d\u51e0\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pd.read_csv()<\/code>\u51fd\u6570\u652f\u6301\u591a\u79cd\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6307\u5b9a\u5206\u9694\u7b26\u3001\u7f3a\u5931\u503c\u6807\u8bb0\u3001\u5217\u540d\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;data.csv&#39;, delimiter=&#39;;&#39;, na_values=[&#39;NA&#39;, &#39;?&#39;], names=[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001NUMPY\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u578b\u6570\u636e\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528<code>np.loadtxt()<\/code>\u6216<code>np.genfromtxt()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528loadtxt\u8bfb\u53d6<\/strong><\/h2>\n<p>data = np.loadtxt(&#39;data.txt&#39;)<\/p>\n<h2><strong>\u4f7f\u7528genfromtxt\u8bfb\u53d6\uff0c\u5e76\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = np.genfromtxt(&#39;data.txt&#39;, delimiter=&#39;,&#39;, filling_values=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001SQLALCHEMY\u4ece\u6570\u636e\u5e93\u5bfc\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5e93ORM\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u67e5\u8be2\uff0c\u5c06\u7ed3\u679c\u5bfc\u5165\u5230pandas DataFrame\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/database.db&#39;)<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2\uff0c\u5e76\u5c06\u7ed3\u679c\u5bfc\u5165DataFrame<\/strong><\/h2>\n<p>df = pd.read_sql(&#39;SELECT * FROM table_name&#39;, engine)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001xlrd\u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>xlrd\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528<code>xlrd.open_workbook()<\/code>\u51fd\u6570\u6253\u5f00Excel\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>sheet_by_index()<\/code>\u83b7\u53d6\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<h2><strong>\u6253\u5f00Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = xlrd.open_workbook(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>sheet = workbook.sheet_by_index(0)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = []<\/p>\n<p>for row in range(sheet.nrows):<\/p>\n<p>    data.append(sheet.row_values(row))<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u6570\u636e\u6e90\u5bfc\u5165<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0cPython\u8fd8\u652f\u6301\u5bfc\u5165\u5176\u4ed6\u6570\u636e\u6e90\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528json\u5e93\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u4f7f\u7528h5py\u8bfb\u53d6HDF5\u6587\u4ef6\uff0c\u4f7f\u7528xml.etree.ElementTree\u89e3\u6790XML\u6587\u4ef6\uff0c\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    data = json.load(f)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<h2><strong>\u8bfb\u53d6HDF5\u6587\u4ef6<\/strong><\/h2>\n<p>with h5py.File(&#39;data.h5&#39;, &#39;r&#39;) as f:<\/p>\n<p>    data = f[&#39;dataset_name&#39;][:]<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u89e3\u6790XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.parse(&#39;data.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.attrib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528Requests\u5e93\u4eceWeb\u5bfc\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728Web\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u4eceWeb\u5bfc\u5165\u6570\u636e\u3002requests\u5e93\u53ef\u4ee5\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<h2><strong>\u89e3\u6790JSON\u6570\u636e<\/strong><\/h2>\n<p>data = response.json()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u5bfc\u5165\u6570\u636e\u96c6\uff0c\u5305\u62ec\u4f7f\u7528pandas\u3001numpy\u3001SQLAlchemy\u3001xlrd\u3001json\u3001h5py\u3001xml.etree.ElementTree\u3001requests\u7b49\u5e93\u3002\u6839\u636e\u6570\u636e\u6e90\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165CSV\u6587\u4ef6\u4f5c\u4e3a\u6570\u636e\u96c6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8f7b\u677e\u5bfc\u5165CSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pandas\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pandas<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u5bfc\u5165CSV\u6587\u4ef6\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u4f7f\u7528<code>pd.read_csv(&#39;\u6587\u4ef6\u8def\u5f84&#39;)<\/code>\uff0c\u5176\u4e2d<code>pd<\/code>\u662fPandas\u7684\u522b\u540d\u3002\u5bfc\u5165\u540e\uff0c\u6570\u636e\u5c06\u4ee5DataFrame\u683c\u5f0f\u5b58\u50a8\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5bfc\u5165Excel\u6587\u4ef6\u7684\u6570\u636e\u96c6\u6709\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/strong><br \/>\u9664\u4e86CSV\u6587\u4ef6\uff0cPython\u8fd8\u652f\u6301\u5bfc\u5165Excel\u6587\u4ef6\u3002\u4f7f\u7528Pandas\u5e93\u7684<code>pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84&#39;)<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u786e\u4fdd\u5b89\u88c5\u4e86<code>openpyxl<\/code>\u6216<code>xlrd<\/code>\u5e93\uff0c\u4ee5\u4fbf\u652f\u6301\u4e0d\u540c\u7248\u672c\u7684Excel\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u5bfc\u5165\u7684\u5de5\u4f5c\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u6570\u636e\u5e93\u4e2d\u5bfc\u5165\u6570\u636e\u96c6\u5230Python\uff1f<\/strong><br \/>\u4ece\u6570\u636e\u5e93\u5bfc\u5165\u6570\u636e\u96c6\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528SQLAlchemy\u5e93\u548cPandas\u7ed3\u5408\u7684\u65b9\u5f0f\u3002\u9996\u5148\uff0c\u5b89\u88c5SQLAlchemy\u5e93\u540e\uff0c\u901a\u8fc7\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7136\u540e\u4f7f\u7528<code>pd.read_sql(&#39;SQL\u67e5\u8be2\u8bed\u53e5&#39;, con)<\/code>\u6765\u76f4\u63a5\u5bfc\u5165\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u5c06\u6570\u636e\u5e93\u4e2d\u590d\u6742\u7684\u6570\u636e\u96c6\u8f7b\u677e\u8f6c\u6362\u4e3aPandas DataFrame\uff0c\u4fbf\u4e8e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u96c6\u5bfc\u5165\uff0c\u5305\u62ec\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3001\u4f7f\u7528numpy\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1057927,"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\/1057919"}],"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=1057919"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057919\/revisions"}],"predecessor-version":[{"id":1057930,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057919\/revisions\/1057930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057927"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}