{"id":1015549,"date":"2024-12-27T12:08:41","date_gmt":"2024-12-27T04:08:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1015549.html"},"modified":"2024-12-27T12:08:43","modified_gmt":"2024-12-27T04:08:43","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e6%89%be%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1015549.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u627e\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095349\/6608aff7-48b3-4267-815c-5291aa617cda.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u627e\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5229\u7528Python\u627e\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u83b7\u53d6\u7f51\u9875\u6570\u636e\u3001\u5229\u7528API\u63a5\u53e3\u83b7\u53d6\u7ed3\u6784\u5316\u6570\u636e\u3001\u4f7f\u7528Python\u5e93\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u83b7\u53d6\u7f51\u9875\u6570\u636e<\/strong>\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u7f16\u5199\u722c\u866b\u811a\u672c\uff0cPython\u53ef\u4ee5\u81ea\u52a8\u8bbf\u95ee\u7f51\u9875\u5e76\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5728\u6ca1\u6709API\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u83b7\u53d6\u6570\u636e\u3002\u7f51\u7edc\u722c\u866b\u901a\u5e38\u4f7f\u7528\u5e93\u5982BeautifulSoup\u548cScrapy\uff0c\u53ef\u4ee5\u5e2e\u52a9\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0c\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u67e5\u627e\u548c\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u83b7\u53d6\u7f51\u9875\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u8bbf\u95ee\u7f51\u9875\u5e76\u63d0\u53d6\u4fe1\u606f\u7684\u7a0b\u5e8f\uff0c\u901a\u5e38\u7528\u4e8e\u6536\u96c6\u4e92\u8054\u7f51\u4e0a\u7684\u5404\u79cd\u6570\u636e\u3002Python\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u722c\u866b\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u662fPython\u4e2d\u4e00\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u7528\u4e8e\u4eceHTML\u548cXML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u4e3a\u590d\u6742\u7684HTML\u6587\u6863\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u5bfc\u822a\u3001\u641c\u7d22\u548c\u4fee\u6539\u6587\u6863\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5BeautifulSoup<\/strong>: \u9996\u5148\u9700\u8981\u5b89\u88c5BeautifulSoup\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install beautifulsoup4<\/code>\u3002<\/li>\n<li><strong>\u89e3\u6790\u7f51\u9875<\/strong>: \u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u7684\u94fe\u63a5<\/strong><\/h2>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790HTML<\/strong>: BeautifulSoup\u4f1a\u5c06\u7f51\u9875\u7ed3\u6784\u5316\u4e3a\u4e00\u4e2a\u6811\u7ed3\u6784\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u627e\u5230\u6211\u4eec\u60f3\u8981\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528Scrapy\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u91c7\u96c6<\/h4>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u4e3a\u722c\u53d6\u7f51\u7ad9\u6570\u636e\u3001\u63d0\u53d6\u6709\u7528\u4fe1\u606f\u800c\u8bbe\u8ba1\u7684\u5e94\u7528\u6846\u67b6\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\u91c7\u96c6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5Scrapy<\/strong>: \u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install scrapy<\/code>\u3002<\/li>\n<li><strong>\u521b\u5efaScrapy\u9879\u76ee<\/strong>: \u4f7f\u7528\u547d\u4ee4\u884c\u521b\u5efaScrapy\u9879\u76ee\uff1a<code>scrapy startproject myproject<\/code>\u3002<\/li>\n<li><strong>\u7f16\u5199\u722c\u866b<\/strong>: \u5728spiders\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u722c\u866b\u811a\u672c\uff0c\u7528\u6765\u5b9a\u4e49\u722c\u53d6\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>class MySpider(scrapy.Spider):<\/p>\n<p>    name = &#39;my_spider&#39;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        for quote in response.css(&#39;div.quote&#39;):<\/p>\n<p>            yield {<\/p>\n<p>                &#39;text&#39;: quote.css(&#39;span.text::text&#39;).get(),<\/p>\n<p>                &#39;author&#39;: quote.css(&#39;span.author::text&#39;).get(),<\/p>\n<p>            }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd0\u884c\u722c\u866b<\/strong>: \u4f7f\u7528\u547d\u4ee4<code>scrapy crawl my_spider<\/code>\u6765\u8fd0\u884c\u722c\u866b\uff0c\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528API\u63a5\u53e3\u83b7\u53d6\u7ed3\u6784\u5316\u6570\u636e<\/h3>\n<\/p>\n<p><p>API\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\u662f\u6307\u8f6f\u4ef6\u7cfb\u7edf\u4e0d\u540c\u7ec4\u6210\u90e8\u5206\u4e4b\u95f4\u7684\u63a5\u53e3\u3002\u8bb8\u591a\u7f51\u7ad9\u548c\u670d\u52a1\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528REST API\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>REST API\u662f\u4e00\u79cd\u5e38\u89c1\u7684Web API\uff0c\u5141\u8bb8\u4f60\u901a\u8fc7HTTP\u8bf7\u6c42\u6765\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6API\u5bc6\u94a5<\/strong>: \u5f88\u591aAPI\u9700\u8981\u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/li>\n<li><strong>\u53d1\u9001\u8bf7\u6c42<\/strong>: \u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>api_url = &#39;https:\/\/api.example.com\/data&#39;<\/p>\n<p>response = requests.get(api_url, headers={&#39;Authorization&#39;: &#39;Bearer YOUR_API_KEY&#39;})<\/p>\n<p>data = response.json()<\/p>\n<p>for item in data:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790JSON\u6570\u636e<\/strong>: \u5927\u591a\u6570API\u4f1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>.json()<\/code>\u65b9\u6cd5\u5c06\u54cd\u5e94\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528GraphQL API\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>GraphQL\u662f\u4e00\u79cd\u7528\u4e8eAPI\u7684\u67e5\u8be2\u8bed\u8a00\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u6307\u5b9a\u6240\u9700\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5Python GraphQL\u5ba2\u6237\u7aef<\/strong>: \u53ef\u4ee5\u4f7f\u7528<code>pip install gql<\/code>\u5b89\u88c5\u3002<\/li>\n<li><strong>\u67e5\u8be2\u6570\u636e<\/strong>: \u4f7f\u7528GraphQL\u5ba2\u6237\u7aef\u53d1\u9001\u67e5\u8be2\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from gql import gql, Client<\/p>\n<p>from gql.transport.requests import RequestsHTTPTransport<\/p>\n<p>transport = RequestsHTTPTransport(url=&#39;https:\/\/api.example.com\/graphql&#39;)<\/p>\n<p>client = Client(transport=transport, fetch_schema_from_transport=True)<\/p>\n<p>query = gql(&#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>  allData {<\/p>\n<p>    id<\/p>\n<p>    name<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<p>result = client.execute(query)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7075\u6d3b\u67e5\u8be2<\/strong>: GraphQL\u5141\u8bb8\u5ba2\u6237\u7aef\u4ec5\u8bf7\u6c42\u6240\u9700\u7684\u6570\u636e\uff0c\u51cf\u5c11\u4e86\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Python\u5e93\u8bfb\u53d6\u672c\u5730\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\u7684\u8bfb\u53d6\uff0c\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u7b49\u5e93\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>CSV\uff08\u9017\u53f7\u5206\u9694\u503c\uff09\u662f\u6700\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u4e4b\u4e00\u3002Python\u7684pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5pandas<\/strong>: \u4f7f\u7528<code>pip install pandas<\/code>\u5b89\u88c5\u3002<\/li>\n<li><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong>: \u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6570\u636e\u5904\u7406<\/strong>: pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3001\u6392\u5e8f\u3001\u805a\u5408\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Excel\u6587\u4ef6\u5728\u6570\u636e\u5206\u6790\u4e2d\u4e5f\u975e\u5e38\u5e38\u89c1\uff0cPython\u53ef\u4ee5\u4f7f\u7528pandas\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5openpyxl<\/strong>: \u4f7f\u7528<code>pip install openpyxl<\/code>\u5b89\u88c5\uff0cpandas\u4f9d\u8d56\u4e8eopenpyxl\u5e93\u6765\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/li>\n<li><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong>: \u4f7f\u7528pandas\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u591a\u8868\u64cd\u4f5c<\/strong>: pandas\u652f\u6301\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u591a\u4e2a\u8868\u683c\uff0c\u5e76\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u5408\u5e76\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u6e05\u6d17\u4e0e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6307\u53bb\u9664\u6216\u4fee\u6b63\u6570\u636e\u4e2d\u7684\u9519\u8bef\u6216\u4e0d\u4e00\u81f4\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7f3a\u5931\u503c\u5904\u7406<\/strong>: \u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>dropna()<\/code>\u548c<code>fillna()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002<\/li>\n<li><strong>\u91cd\u590d\u503c\u5904\u7406<\/strong>: \u4f7f\u7528<code>drop_duplicates()<\/code>\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">df = df.dropna()  # \u53bb\u9664\u7f3a\u5931\u503c<\/p>\n<p>df = df.drop_duplicates()  # \u53bb\u9664\u91cd\u590d\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5f02\u5e38\u503c\u68c0\u6d4b<\/strong>: \u53ef\u4ee5\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u6216\u53ef\u89c6\u5316\u5de5\u5177\u68c0\u6d4b\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u6570\u636e\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u8f6c\u6362\u662f\u6307\u5c06\u6570\u636e\u4ece\u4e00\u79cd\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong>: \u4f7f\u7528pandas\u7684<code>astype()<\/code>\u65b9\u6cd5\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>\u6570\u636e\u683c\u5f0f\u5316<\/strong>: \u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;] = df[&#39;column_name&#39;].astype(&#39;int&#39;)  # \u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/p>\n<p>df[&#39;date&#39;] = pd.to_datetime(df[&#39;date&#39;])  # \u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7279\u5f81\u5de5\u7a0b<\/strong>: \u53ef\u4ee5\u901a\u8fc7\u7279\u5f81\u63d0\u53d6\u548c\u7279\u5f81\u9009\u62e9\u7b49\u65b9\u6cd5\u63d0\u9ad8\u6570\u636e\u7684\u8d28\u91cf\u548c\u5206\u6790\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u5171\u4eab<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u9700\u8981\u5c06\u5176\u5b58\u50a8\u6216\u5171\u4eab\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u6570\u636e\u5b58\u50a8<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u5b58\u50a8\u4e3aCSV\u3001Excel\u3001\u6570\u636e\u5e93\u7b49\u591a\u79cd\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b58\u50a8\u4e3aCSV\u6587\u4ef6<\/strong>: \u4f7f\u7528pandas\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5b58\u50a8\u4e3aExcel\u6587\u4ef6<\/strong>: \u4f7f\u7528pandas\u7684<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">df.to_csv(&#39;clean_data.csv&#39;, index=False)  # \u5b58\u50a8\u4e3aCSV\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;clean_data.xlsx&#39;, index=False)  # \u5b58\u50a8\u4e3aExcel\u6587\u4ef6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/strong>: \u53ef\u4ee5\u4f7f\u7528SQLAlchemy\u7b49\u5e93\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u6570\u636e\u5171\u4eab<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7API\u3001\u6587\u4ef6\u5171\u4eab\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u65b9\u5f0f\u5171\u4eab\u6570\u636e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u901a\u8fc7API\u5171\u4eab<\/strong>: \u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\u521b\u5efaAPI\u63a5\u53e3\uff0c\u63d0\u4f9b\u6570\u636e\u8bbf\u95ee\u3002<\/li>\n<li><strong>\u6587\u4ef6\u5171\u4eab<\/strong>: \u53ef\u4ee5\u4f7f\u7528\u4e91\u5b58\u50a8\u6216FTP\u7b49\u65b9\u5f0f\u5171\u4eab\u6570\u636e\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p><strong>\u6570\u636e\u5b89\u5168<\/strong>: \u5728\u5171\u4eab\u6570\u636e\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u9690\u79c1\u4fdd\u62a4\uff0c\u786e\u4fdd\u6570\u636e\u4e0d\u88ab\u672a\u6388\u6743\u7684\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u67e5\u627e\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u67e5\u627e\u7684\u7b2c\u4e00\u6b65\u662f\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff0c\u4f8b\u5982Pandas\u548cNumPy\u3002Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u3001\u8fc7\u6ee4\u548c\u5904\u7406\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>pd.read_csv()<\/code>\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u6216\u4f7f\u7528<code>pd.DataFrame()<\/code>\u521b\u5efa\u6570\u636e\u6846\u3002\u63a5\u4e0b\u6765\uff0c\u5229\u7528\u6570\u636e\u6846\u7684\u7b5b\u9009\u529f\u80fd\uff0c\u5982\u6761\u4ef6\u8fc7\u6ee4\u548c\u5206\u7ec4\u7edf\u8ba1\uff0c\u6765\u627e\u5230\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u636e\u67e5\u627e\u65b9\u6cd5\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u67e5\u627e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>loc<\/code>\u548c<code>iloc<\/code>\u8fdb\u884c\u884c\u5217\u9009\u62e9\uff0c\u5229\u7528\u5e03\u5c14\u7d22\u5f15\u7b5b\u9009\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u901a\u8fc7<code>query()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u7b5b\u9009\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>groupby()<\/code>\u53ef\u4ee5\u5728\u5206\u7c7b\u6570\u636e\u4e0a\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u6709\u6548\u5730\u5206\u6790\u548c\u67e5\u627e\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u4ee5\u4f18\u5316\u6570\u636e\u67e5\u627e\u8fc7\u7a0b\uff1f<\/strong><br \/>\u5728\u6570\u636e\u67e5\u627e\u8fc7\u7a0b\u4e2d\uff0c\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u4e2d\u7684<code>isnull()<\/code>\u548c<code>dropna()<\/code>\u65b9\u6cd5\u6765\u8bc6\u522b\u548c\u5220\u9664\u7f3a\u5931\u503c\u3002\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\uff0c\u4f8b\u5982\u7528\u5e73\u5747\u6570\u6216\u4e2d\u4f4d\u6570\u66ff\u4ee3\u3002\u8fd9\u79cd\u9884\u5904\u7406\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u4e5f\u4f7f\u540e\u7eed\u7684\u6570\u636e\u67e5\u627e\u66f4\u52a0\u987a\u5229\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u627e\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u83b7\u53d6\u7f51\u9875\u6570\u636e\u3001\u5229\u7528API\u63a5\u53e3\u83b7\u53d6\u7ed3\u6784\u5316\u6570\u636e\u3001\u4f7f\u7528Python\u5e93\u8bfb [&hellip;]","protected":false},"author":3,"featured_media":1015554,"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\/1015549"}],"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=1015549"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015549\/revisions"}],"predecessor-version":[{"id":1015555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015549\/revisions\/1015555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1015554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1015549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1015549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1015549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}