{"id":1120739,"date":"2025-01-08T19:00:09","date_gmt":"2025-01-08T11:00:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120739.html"},"modified":"2025-01-08T19:00:14","modified_gmt":"2025-01-08T11:00:14","slug":"%e5%a6%82%e4%bd%95%e6%8a%8apython%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e5%af%bc%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120739.html","title":{"rendered":"\u5982\u4f55\u628apython\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083242\/fd249552-72f5-41da-abb0-c01ace947573.webp\" alt=\"\u5982\u4f55\u628apython\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u5c06Python\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u6765\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u6587\u4ef6I\/O\u64cd\u4f5c\u3001\u4f7f\u7528\u6570\u636e\u5e93\u3001\u4f7f\u7528\u4e91\u5b58\u50a8\u670d\u52a1\u4ee5\u53ca\u4f7f\u7528\u7f51\u7edcAPI\u3002<\/strong> \u5e38\u7528\u7684\u6280\u672f\u6709\uff1a<strong>CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001JSON\u6587\u4ef6\u3001SQL\u6570\u636e\u5e93\u3001NoSQL\u6570\u636e\u5e93\uff08\u5982MongoDB\uff09\u3001RESTful API\u3001\u4e91\u5b58\u50a8\u670d\u52a1\uff08\u5982AWS S3\uff09<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\uff0c\u5e2e\u52a9\u8bfb\u8005\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u5f0f\u6765\u5bfc\u51faPython\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u7279\u522b\u9002\u5408\u7528\u6765\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528csv\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5199\u5165CSV\u6587\u4ef6\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, &quot;New York&quot;],<\/p>\n<p>    [&quot;Bob&quot;, 25, &quot;San Francisco&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, 35, &quot;Los Angeles&quot;]<\/p>\n<p>]<\/p>\n<p>with open(&quot;output.csv&quot;, &quot;w&quot;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868<code>data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>csv.writer<\/code>\u5c06\u5176\u5199\u5165\u5230<code>output.csv<\/code>\u6587\u4ef6\u4e2d\u3002<strong>\u6ce8\u610f\uff0c\u4f7f\u7528<code>newline=&#39;&#39;<\/code>\u53c2\u6570\u53ef\u4ee5\u907f\u514d\u5728Windows\u4e0a\u51fa\u73b0\u591a\u4f59\u7684\u7a7a\u884c\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u652f\u6301\u5c06DataFrame\u76f4\u63a5\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;San Francisco&quot;, &quot;Los Angeles&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.to_csv(&quot;output.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u683c\u5f0f\u7684\u6570\u636e\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u6700\u540e\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002<strong><code>index=False<\/code>\u53c2\u6570\u7528\u4e8e\u4e0d\u5bfc\u51fa\u884c\u7d22\u5f15\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e8c\u3001Excel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>Excel\u6587\u4ef6\uff08\u901a\u5e38\u4e3a<code>.xlsx<\/code>\u683c\u5f0f\uff09\u5728\u5546\u52a1\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u975e\u5e38\u6d41\u884c\u3002Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528openpyxl\u5e93<\/h3>\n<\/p>\n<p><p><code>openpyxl<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u8bfb\u5199Excel\u6587\u4ef6\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<p>data = [<\/p>\n<p>    [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, &quot;New York&quot;],<\/p>\n<p>    [&quot;Bob&quot;, 25, &quot;San Francisco&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, 35, &quot;Los Angeles&quot;]<\/p>\n<p>]<\/p>\n<p>wb = Workbook()<\/p>\n<p>ws = wb.active<\/p>\n<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<p>wb.save(&quot;output.xlsx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>Workbook<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06\u6570\u636e\u9010\u884c\u6dfb\u52a0\u5230\u6d3b\u52a8\u5de5\u4f5c\u8868\u4e2d\uff0c\u6700\u540e\u4fdd\u5b58\u4e3a<code>output.xlsx<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u4e5f\u652f\u6301\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;San Francisco&quot;, &quot;Los Angeles&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.to_excel(&quot;output.xlsx&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002<strong>\u540c\u6837\u4f7f\u7528<code>index=False<\/code>\u53c2\u6570\u4e0d\u5bfc\u51fa\u884c\u7d22\u5f15\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e09\u3001JSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u5904\u7406JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528json\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;people&quot;: [<\/p>\n<p>        {&quot;Name&quot;: &quot;Alice&quot;, &quot;Age&quot;: 30, &quot;City&quot;: &quot;New York&quot;},<\/p>\n<p>        {&quot;Name&quot;: &quot;Bob&quot;, &quot;Age&quot;: 25, &quot;City&quot;: &quot;San Francisco&quot;},<\/p>\n<p>        {&quot;Name&quot;: &quot;Charlie&quot;, &quot;Age&quot;: 35, &quot;City&quot;: &quot;Los Angeles&quot;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p>with open(&quot;output.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178\u683c\u5f0f\u7684\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>json.dump<\/code>\u65b9\u6cd5\u5c06\u5176\u5199\u5165\u5230<code>output.json<\/code>\u6587\u4ef6\u4e2d\u3002<strong><code>indent=4<\/code>\u53c2\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7fJSON\u6587\u4ef6\u66f4\u6613\u8bfb\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u4e5f\u652f\u6301\u5c06DataFrame\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;San Francisco&quot;, &quot;Los Angeles&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.to_json(&quot;output.json&quot;, orient=&quot;records&quot;, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>to_json<\/code>\u65b9\u6cd5\u5c06DataFrame\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\u3002<strong><code>orient=&quot;records&quot;<\/code>\u53c2\u6570\u6307\u5b9a\u6bcf\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2aJSON\u5bf9\u8c61\uff0c<code>indent=4<\/code>\u53c2\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u56db\u3001SQL\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>SQL\u6570\u636e\u5e93\uff08\u5982SQLite\u3001MySQL\u3001PostgreSQL\uff09\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\u3002Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u4e0eSQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528sqlite3\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>sqlite3<\/code>\u662fPython\u5185\u7f6e\u7684SQLite\u6570\u636e\u5e93\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, 30, &quot;New York&quot;),<\/p>\n<p>    (&quot;Bob&quot;, 25, &quot;San Francisco&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, 35, &quot;Los Angeles&quot;)<\/p>\n<p>]<\/p>\n<p>conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>cur = conn.cursor()<\/p>\n<p>cur.execute(&quot;CREATE TABLE IF NOT EXISTS people (Name TEXT, Age INTEGER, City TEXT)&quot;)<\/p>\n<p>cur.executemany(&quot;INSERT INTO people (Name, Age, City) VALUES (?, ?, ?)&quot;, data)<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fde\u63a5\u5230\u4e00\u4e2aSQLite\u6570\u636e\u5e93\uff08\u5982\u679c\u4e0d\u5b58\u5728\u5c06\u4f1a\u521b\u5efa\uff09\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u8868\uff0c\u5e76\u5c06\u6570\u636e\u63d2\u5165\u5230\u8868\u4e2d\u3002<strong><code>executemany<\/code>\u65b9\u6cd5\u7528\u4e8e\u4e00\u6b21\u6027\u63d2\u5165\u591a\u884c\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528SQLAlchemy\u5e93<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff08ORM\uff09\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String, Sequence<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>Base = declarative_base()<\/p>\n<p>class Person(Base):<\/p>\n<p>    __tablename__ = &#39;people&#39;<\/p>\n<p>    id = Column(Integer, Sequence(&#39;person_id_seq&#39;), primary_key=True)<\/p>\n<p>    name = Column(String(50))<\/p>\n<p>    age = Column(Integer)<\/p>\n<p>    city = Column(String(50))<\/p>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)<\/p>\n<p>Base.metadata.create_all(engine)<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p>data = [<\/p>\n<p>    Person(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;),<\/p>\n<p>    Person(name=&quot;Bob&quot;, age=25, city=&quot;San Francisco&quot;),<\/p>\n<p>    Person(name=&quot;Charlie&quot;, age=35, city=&quot;Los Angeles&quot;)<\/p>\n<p>]<\/p>\n<p>session.add_all(data)<\/p>\n<p>session.commit()<\/p>\n<p>session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SQLAlchemy\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Person<\/code>\u7c7b\uff0c\u5e76\u5c06\u6570\u636e\u63d2\u5165\u5230SQLite\u6570\u636e\u5e93\u4e2d\u3002<strong>SQLAlchemy\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u5982\u5173\u7cfb\u6620\u5c04\u548c\u67e5\u8be2\u6784\u5efa\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e94\u3001NoSQL\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>NoSQL\u6570\u636e\u5e93\uff08\u5982MongoDB\u3001Cassandra\uff09\u63d0\u4f9b\u4e86\u4e0d\u540c\u4e8e\u4f20\u7edfSQL\u6570\u636e\u5e93\u7684\u5b58\u50a8\u65b9\u5f0f\u3002Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u4e0eNoSQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528PyMongo\u5e93<\/h3>\n<\/p>\n<p><p><code>PyMongo<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u4e0eMongoDB\u8fdb\u884c\u4ea4\u4e92\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pymongo import MongoClient<\/p>\n<p>data = [<\/p>\n<p>    {&quot;Name&quot;: &quot;Alice&quot;, &quot;Age&quot;: 30, &quot;City&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;Name&quot;: &quot;Bob&quot;, &quot;Age&quot;: 25, &quot;City&quot;: &quot;San Francisco&quot;},<\/p>\n<p>    {&quot;Name&quot;: &quot;Charlie&quot;, &quot;Age&quot;: 35, &quot;City&quot;: &quot;Los Angeles&quot;}<\/p>\n<p>]<\/p>\n<p>client = MongoClient(&quot;mongodb:\/\/localhost:27017\/&quot;)<\/p>\n<p>db = client[&quot;example_db&quot;]<\/p>\n<p>collection = db[&quot;people&quot;]<\/p>\n<p>collection.insert_many(data)<\/p>\n<p>client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fde\u63a5\u5230MongoDB\uff0c\u7136\u540e\u5c06\u6570\u636e\u63d2\u5165\u5230\u4e00\u4e2a\u96c6\u5408\u4e2d\u3002<strong><code>insert_many<\/code>\u65b9\u6cd5\u7528\u4e8e\u4e00\u6b21\u6027\u63d2\u5165\u591a\u6761\u6587\u6863\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528Cassandra\u9a71\u52a8\u5e93<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>cassandra-driver<\/code>\u5e93\u4e0eCassandra\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cassandra.cluster import Cluster<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, 30, &quot;New York&quot;),<\/p>\n<p>    (&quot;Bob&quot;, 25, &quot;San Francisco&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, 35, &quot;Los Angeles&quot;)<\/p>\n<p>]<\/p>\n<p>cluster = Cluster()<\/p>\n<p>session = cluster.connect()<\/p>\n<p>session.execute(&quot;CREATE KEYSPACE IF NOT EXISTS example WITH replication = {&#39;class&#39;: &#39;SimpleStrategy&#39;, &#39;replication_factor&#39;: 1}&quot;)<\/p>\n<p>session.execute(&quot;CREATE TABLE IF NOT EXISTS example.people (Name text, Age int, City text, PRIMARY KEY (Name))&quot;)<\/p>\n<p>for item in data:<\/p>\n<p>    session.execute(&quot;INSERT INTO example.people (Name, Age, City) VALUES (%s, %s, %s)&quot;, item)<\/p>\n<p>cluster.shutdown()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fde\u63a5\u5230Cassandra\u6570\u636e\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u952e\u7a7a\u95f4\u548c\u8868\uff0c\u5e76\u5c06\u6570\u636e\u63d2\u5165\u5230\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001RESTful API<\/h2>\n<\/p>\n<p><p>RESTful API\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u4ea4\u6362\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u8de8\u5e73\u53f0\u7684\u6570\u636e\u4f20\u8f93\u3002Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u4e0eRESTful API\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528requests\u5e93<\/h3>\n<\/p>\n<p><p><code>requests<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u5e38\u7528\u4e8e\u4e0eRESTful API\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>data = [<\/p>\n<p>    {&quot;Name&quot;: &quot;Alice&quot;, &quot;Age&quot;: 30, &quot;City&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;Name&quot;: &quot;Bob&quot;, &quot;Age&quot;: 25, &quot;City&quot;: &quot;San Francisco&quot;},<\/p>\n<p>    {&quot;Name&quot;: &quot;Charlie&quot;, &quot;Age&quot;: 35, &quot;City&quot;: &quot;Los Angeles&quot;}<\/p>\n<p>]<\/p>\n<p>response = requests.post(&quot;http:\/\/example.com\/api\/people&quot;, json=data)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Data successfully sent to API&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to send data to API&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.post<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u4ee5JSON\u683c\u5f0f\u53d1\u9001\u5230\u4e00\u4e2aRESTful API\u7aef\u70b9\u3002<strong>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u662f\u5426\u6210\u529f\u53d1\u9001\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528Flask\u5e93\u521b\u5efaAPI<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>Flask<\/code>\u5e93\u521b\u5efa\u7b80\u5355RESTful API\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>data = []<\/p>\n<p>@app.route(&#39;\/api\/people&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def add_people():<\/p>\n<p>    new_data = request.get_json()<\/p>\n<p>    data.extend(new_data)<\/p>\n<p>    return jsonify({&quot;message&quot;: &quot;Data successfully added&quot;}), 200<\/p>\n<p>@app.route(&#39;\/api\/people&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_people():<\/p>\n<p>    return jsonify(data), 200<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u4f9b\u4e24\u4e2aAPI\u7aef\u70b9\uff1a\u4e00\u4e2a\u7528\u4e8e\u6dfb\u52a0\u6570\u636e\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4e91\u5b58\u50a8\u670d\u52a1<\/h2>\n<\/p>\n<p><p>\u4e91\u5b58\u50a8\u670d\u52a1\uff08\u5982AWS S3\u3001Google Cloud Storage\uff09\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b58\u50a8\u548c\u5206\u53d1\u80fd\u529b\u3002Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u4e0e\u4e91\u5b58\u50a8\u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528boto3\u5e93<\/h3>\n<\/p>\n<p><p><code>boto3<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u4e0eAWS\u670d\u52a1\uff08\u5982S3\uff09\u8fdb\u884c\u4ea4\u4e92\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import boto3<\/p>\n<p>from botocore.exceptions import NoCredentialsError<\/p>\n<p>data = &quot;Hello, world!&quot;<\/p>\n<p>s3 = boto3.client(&#39;s3&#39;)<\/p>\n<p>try:<\/p>\n<p>    s3.put_object(Bucket=&#39;your-bucket-name&#39;, Key=&#39;example.txt&#39;, Body=data)<\/p>\n<p>    print(&quot;Data successfully uploaded to S3&quot;)<\/p>\n<p>except NoCredentialsError:<\/p>\n<p>    print(&quot;Credentials not available&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>boto3.client<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2aS3\u5ba2\u6237\u7aef\uff0c\u5e76\u5c06\u6570\u636e\u4e0a\u4f20\u5230\u6307\u5b9a\u7684S3\u6876\u4e2d\u3002<strong>\u786e\u4fdd\u914d\u7f6e\u4e86AWS\u51ed\u8bc1\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528google-cloud-storage\u5e93<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>google-cloud-storage<\/code>\u5e93\u4e0eGoogle Cloud Storage\u8fdb\u884c\u4ea4\u4e92\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from google.cloud import storage<\/p>\n<p>data = &quot;Hello, world!&quot;<\/p>\n<p>client = storage.Client()<\/p>\n<p>bucket = client.bucket(&#39;your-bucket-name&#39;)<\/p>\n<p>blob = bucket.blob(&#39;example.txt&#39;)<\/p>\n<p>blob.upload_from_string(data)<\/p>\n<p>print(&quot;Data successfully uploaded to Google Cloud Storage&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aGoogle Cloud Storage\u5ba2\u6237\u7aef\uff0c\u7136\u540e\u5c06\u6570\u636e\u4e0a\u4f20\u5230\u6307\u5b9a\u7684\u6876\u4e2d\u3002<strong>\u786e\u4fdd\u914d\u7f6e\u4e86Google Cloud\u51ed\u8bc1\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u591a\u79cd\u5c06Python\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u6765\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u6587\u4ef6I\/O\u64cd\u4f5c\uff08\u5982CSV\u3001Excel\u3001JSON\u6587\u4ef6\uff09\u3001\u4f7f\u7528\u6570\u636e\u5e93\uff08\u5982SQL\u3001NoSQL\u6570\u636e\u5e93\uff09\u3001\u4f7f\u7528RESTful API\u4ee5\u53ca\u4f7f\u7528\u4e91\u5b58\u50a8\u670d\u52a1\uff08\u5982AWS S3\u3001Google Cloud Storage\uff09\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5bfc\u51fa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u800c\u8a00\u4e4b\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u5bfc\u51fa\u6570\u636e\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u6570\u636e\u5904\u7406\u7684\u7075\u6d3b\u6027\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6216<code>pandas<\/code>\u5e93\u6765\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u65f6\uff0c\u9996\u5148\u9700\u8981\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>csv.writer<\/code>\u5c06\u6570\u636e\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002\u5982\u679c\u9009\u62e9\u4f7f\u7528<code>pandas<\/code>\uff0c\u53ea\u9700\u5c06\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\uff0c\u8c03\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u5373\u53ef\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u4fdd\u5b58\u548c\u5206\u4eab\u6570\u636e\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u6570\u636e\u683c\u5f0f\u7684\u5bfc\u51fa\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u5bfc\u51fa\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8eCSV\u3001Excel\uff08XLSX\uff09\u3001JSON\u3001SQL\u6570\u636e\u5e93\u3001HTML\u7b49\u3002\u4e0d\u540c\u7684\u5e93\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u5e93\u975e\u5e38\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u5bfc\u51fa\uff0c\u800c<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5b57\u5178\u548c\u5217\u8868\u7b49\u6570\u636e\u7ed3\u6784\u7684\u5bfc\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5bfc\u51fa\u7684\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5bfc\u51fa\u7684\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff0c\u5efa\u8bae\u5728\u5bfc\u51fa\u4e4b\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1\u548c\u6e05\u6d17\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684<code>DataFrame<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u548c\u7f3a\u5931\u503c\uff0c\u786e\u4fdd\u6570\u636e\u7b26\u5408\u9884\u671f\u3002\u6b64\u5916\uff0c\u5728\u5bfc\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u53c2\u6570\u4ee5\u8c03\u6574\u683c\u5f0f\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5206\u9694\u7b26\u3001\u7f16\u7801\u65b9\u5f0f\u7b49\uff0c\u8fdb\u4e00\u6b65\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u6765\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u6587\u4ef6I\/O\u64cd\u4f5c\u3001\u4f7f\u7528\u6570\u636e\u5e93\u3001\u4f7f\u7528\u4e91\u5b58\u50a8\u670d\u52a1\u4ee5\u53ca\u4f7f\u7528\u7f51\u7edcA [&hellip;]","protected":false},"author":3,"featured_media":1120754,"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\/1120739"}],"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=1120739"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120739\/revisions"}],"predecessor-version":[{"id":1120759,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120739\/revisions\/1120759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120754"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}