{"id":1150959,"date":"2025-01-13T17:07:48","date_gmt":"2025-01-13T09:07:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150959.html"},"modified":"2025-01-13T17:07:50","modified_gmt":"2025-01-13T09:07:50","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%af%bc%e5%87%ba%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150959.html","title":{"rendered":"\u5982\u4f55\u7528python\u5bfc\u51fa\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181313\/3ac276bd-6558-4880-9050-8438b61200f2.webp\" alt=\"\u5982\u4f55\u7528python\u5bfc\u51fa\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001JSON\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528pandas\u5e93\u5bfc\u51faCSV\u6587\u4ef6\u3001\u4f7f\u7528openpyxl\u6216xlsxwriter\u5e93\u5bfc\u51faExcel\u6587\u4ef6\u3001\u4f7f\u7528json\u5e93\u5bfc\u51faJSON\u6587\u4ef6\u3001\u4f7f\u7528sqlite3\u5e93\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5bfc\u51fa\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma-Separated Values\uff09\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002Python\u7684pandas\u5e93\u975e\u5e38\u9002\u5408\u5904\u7406\u548c\u5bfc\u51faCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pandas\u5bfc\u51faCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u51faDataFrame\u5230CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u793a\u4f8b\u6570\u636e\u7684\u5b57\u5178\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2apandas DataFrame\uff0c\u6700\u540e\u4f7f\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u5c06DataFrame\u5bfc\u51fa\u5230CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u51fa\u5230Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Excel\u6587\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u7535\u5b50\u8868\u683c\u683c\u5f0f\uff0cPython\u7684openpyxl\u548cxlsxwriter\u5e93\u90fd\u53ef\u4ee5\u7528\u6765\u5bfc\u51fa\u6570\u636e\u5230Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528openpyxl\u5bfc\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>openpyxl\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>ws.append([&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<p>ws.append([&#39;Alice&#39;, 25, &#39;New York&#39;])<\/p>\n<p>ws.append([&#39;Bob&#39;, 30, &#39;Los Angeles&#39;])<\/p>\n<p>ws.append([&#39;Charlie&#39;, 35, &#39;Chicago&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528xlsxwriter\u5bfc\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>xlsxwriter\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efaExcel 2007\u53ca\u66f4\u9ad8\u7248\u672c\u6587\u4ef6\u7684Python\u5e93\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u826f\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;output.xlsx&#39;)<\/p>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 25, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 30, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<p>row = 0<\/p>\n<p>for record in data:<\/p>\n<p>    col = 0<\/p>\n<p>    for item in record:<\/p>\n<p>        worksheet.write(row, col, item)<\/p>\n<p>        col += 1<\/p>\n<p>    row += 1<\/p>\n<h2><strong>\u5173\u95ed\u5e76\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5bfc\u51fa\u5230JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0cPython\u7684json\u5e93\u975e\u5e38\u9002\u5408\u5bfc\u51fa\u6570\u636e\u5230JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528json\u5e93\u5bfc\u51faJSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;people&#39;: [<\/p>\n<p>        {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;Los Angeles&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;city&#39;: &#39;Chicago&#39;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<h2><strong>\u5bfc\u51fa\u5230JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(data, json_file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u793a\u4f8b\u6570\u636e\u7684\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5bfc\u51fa\u5230JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bfc\u51fa\u5230\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>Python\u7684sqlite3\u5e93\u53ef\u4ee5\u7528\u6765\u64cd\u4f5cSQLite\u6570\u636e\u5e93\uff0c\u5c06\u6570\u636e\u5bfc\u51fa\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528sqlite3\u5bfc\u51fa\u6570\u636e\u5230\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0cPython\u5185\u7f6e\u7684sqlite3\u5e93\u975e\u5e38\u9002\u5408\u64cd\u4f5cSQLite\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5\u548c\u6e38\u6807<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS people<\/p>\n<p>             (name text, age integer, city text)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    (&#39;Alice&#39;, 25, &#39;New York&#39;),<\/p>\n<p>    (&#39;Bob&#39;, 30, &#39;Los Angeles&#39;),<\/p>\n<p>    (&#39;Charlie&#39;, 35, &#39;Chicago&#39;)<\/p>\n<p>]<\/p>\n<p>c.executemany(&#39;INSERT INTO people VALUES (?,?,?)&#39;, data)<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1\u5e76\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aSQLite\u6570\u636e\u5e93\u8fde\u63a5\u548c\u6e38\u6807\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u5f20\u540d\u4e3a<code>people<\/code>\u7684\u8868\uff0c\u63a5\u7740\u4f7f\u7528<code>executemany()<\/code>\u65b9\u6cd5\u63d2\u5165\u6570\u636e\uff0c\u6700\u540e\u63d0\u4ea4\u4e8b\u52a1\u5e76\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5bfc\u51fa\u5230\u5176\u4ed6\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u89c1\u7684\u6587\u4ef6\u683c\u5f0f\uff0cPython\u8fd8\u53ef\u4ee5\u5c06\u6570\u636e\u5bfc\u51fa\u5230\u5176\u4ed6\u683c\u5f0f\uff0c\u5982HTML\u3001XML\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pandas\u5bfc\u51fa\u5230HTML\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230HTML\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_html(&#39;output.html&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528xml.etree.ElementTree\u5bfc\u51fa\u5230XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = ET.Element(&#39;people&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20<\/strong><\/h2>\n<p>person1 = ET.SubElement(root, &#39;person&#39;)<\/p>\n<p>ET.SubElement(person1, &#39;name&#39;).text = &#39;Alice&#39;<\/p>\n<p>ET.SubElement(person1, &#39;age&#39;).text = &#39;25&#39;<\/p>\n<p>ET.SubElement(person1, &#39;city&#39;).text = &#39;New York&#39;<\/p>\n<p>person2 = ET.SubElement(root, &#39;person&#39;)<\/p>\n<p>ET.SubElement(person2, &#39;name&#39;).text = &#39;Bob&#39;<\/p>\n<p>ET.SubElement(person2, &#39;age&#39;).text = &#39;30&#39;<\/p>\n<p>ET.SubElement(person2, &#39;city&#39;).text = &#39;Los Angeles&#39;<\/p>\n<p>person3 = ET.SubElement(root, &#39;person&#39;)<\/p>\n<p>ET.SubElement(person3, &#39;name&#39;).text = &#39;Charlie&#39;<\/p>\n<p>ET.SubElement(person3, &#39;age&#39;).text = &#39;35&#39;<\/p>\n<p>ET.SubElement(person3, &#39;city&#39;).text = &#39;Chicago&#39;<\/p>\n<h2><strong>\u521b\u5efa\u6811\u5bf9\u8c61<\/strong><\/h2>\n<p>tree = ET.ElementTree(root)<\/p>\n<h2><strong>\u4fdd\u5b58\u5230XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree.write(&#39;output.xml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5bfc\u51fa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u5e93\u548c\u5e38\u7528\u7684pandas\u5e93\uff0cPython\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u6765\u5bfc\u51fa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528tablib\u5bfc\u51fa\u6570\u636e\u5230\u591a\u79cd\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>tablib\u662f\u4e00\u4e2a\u6570\u636e\u5bfc\u51fa\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u5982CSV\u3001Excel\u3001JSON\u3001YAML\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tablib<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    (&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;),<\/p>\n<p>    (&#39;Alice&#39;, 25, &#39;New York&#39;),<\/p>\n<p>    (&#39;Bob&#39;, 30, &#39;Los Angeles&#39;),<\/p>\n<p>    (&#39;Charlie&#39;, 35, &#39;Chicago&#39;)<\/p>\n<p>]<\/p>\n<p>dataset = tablib.Dataset(*data, headers=data[0])<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(dataset.export(&#39;csv&#39;))<\/p>\n<h2><strong>\u5bfc\u51fa\u5230Excel\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.xlsx&#39;, &#39;wb&#39;) as f:<\/p>\n<p>    f.write(dataset.export(&#39;xlsx&#39;))<\/p>\n<h2><strong>\u5bfc\u51fa\u5230JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(dataset.export(&#39;json&#39;))<\/p>\n<h2><strong>\u5bfc\u51fa\u5230YAML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.yaml&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(dataset.export(&#39;yaml&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u6570\u636e\u5e93ORM\u5bfc\u51fa\u6570\u636e<\/h3>\n<\/p>\n<p><p>ORM\uff08Object-Relational Mapping\uff09\u662f\u4e00\u79cd\u5c06\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u6620\u5c04\u5230\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0cPython\u7684SQLAlchemy\u5e93\u53ef\u4ee5\u7528\u6765\u5bfc\u51fa\u6570\u636e\u5230\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528SQLAlchemy\u5bfc\u51fa\u6570\u636e\u5230\u6570\u636e\u5e93<\/h4>\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<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;, echo=True)<\/p>\n<h2><strong>\u521b\u5efa\u57fa\u7c7b<\/strong><\/h2>\n<p>Base = declarative_base()<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6a21\u578b<\/strong><\/h2>\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<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>session.add_all([<\/p>\n<p>    Person(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;),<\/p>\n<p>    Person(name=&#39;Bob&#39;, age=30, city=&#39;Los Angeles&#39;),<\/p>\n<p>    Person(name=&#39;Charlie&#39;, age=35, city=&#39;Chicago&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>session.commit()<\/p>\n<h2><strong>\u5173\u95ed\u4f1a\u8bdd<\/strong><\/h2>\n<p>session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SQLAlchemy\u521b\u5efa\u4e86\u4e00\u4e2aSQLite\u6570\u636e\u5e93\u5f15\u64ce\u548c\u57fa\u7c7b\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u636e\u6a21\u578b<code>Person<\/code>\uff0c\u63a5\u7740\u521b\u5efa\u4e86\u8868\u548c\u4f1a\u8bdd\uff0c\u5e76\u6dfb\u52a0\u4e86\u6570\u636e\uff0c\u6700\u540e\u63d0\u4ea4\u4e8b\u52a1\u5e76\u5173\u95ed\u4f1a\u8bdd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u5927\u6570\u636e\u5bfc\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u9700\u8981\u8003\u8651\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u5927\u6570\u636e\u5bfc\u51fa\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u5206\u6279\u6b21\u5bfc\u51fa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5bfc\u51fa\u5927\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6279\u6b21\u5bfc\u51fa\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u548c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;] * 100000,<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35] * 100000,<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;] * 100000<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5206\u6279\u6b21\u5bfc\u51fa\u6570\u636e<\/strong><\/h2>\n<p>batch_size = 10000<\/p>\n<p>num_batches = len(df) \/\/ batch_size<\/p>\n<p>for i in range(num_batches + 1):<\/p>\n<p>    start = i * batch_size<\/p>\n<p>    end = (i + 1) * batch_size<\/p>\n<p>    batch = df[start:end]<\/p>\n<p>    batch.to_csv(f&#39;output_{i}.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Dask\u5904\u7406\u5927\u6570\u636e<\/h4>\n<\/p>\n<p><p>Dask\u662f\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;] * 1000000,<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35] * 1000000,<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;] * 1000000<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDask DataFrame<\/strong><\/h2>\n<p>df = dd.from_pandas(pd.DataFrame(data), npartitions=10)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output_*.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5bfc\u51fa\u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u9a8c\u8bc1\u548c\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u5bfc\u51fa\u6570\u636e\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7ecf\u8fc7\u9a8c\u8bc1\u548c\u6e05\u6d17\uff0c\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;Dave&#39;, &#39;&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35, None, 40],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;, &#39;Houston&#39;, &#39;Miami&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6570\u636e\u9a8c\u8bc1\u548c\u6e05\u6d17<\/strong><\/h2>\n<p>df.dropna(inplace=True)  # \u5220\u9664\u7f3a\u5931\u503c<\/p>\n<p>df = df[df[&#39;Name&#39;] != &#39;&#39;]  # \u5220\u9664\u7a7a\u503c<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;cleaned_output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u6b63\u786e\u5173\u95ed\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;) as f:<\/p>\n<p>    df.to_csv(f, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5230\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001JSON\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5206\u6279\u6b21\u5bfc\u51fa\u6216Dask\u5e93\u6765\u63d0\u9ad8\u6027\u80fd\u548c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u6570\u636e\u5bfc\u51fa\u524d\u7684\u9a8c\u8bc1\u548c\u6e05\u6d17\u4ee5\u53ca\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u90fd\u662f\u786e\u4fdd\u6570\u636e\u5bfc\u51fa\u8fc7\u7a0b\u987a\u5229\u8fdb\u884c\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/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 \/>\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u53ef\u4ee5\u5229\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\uff0c\u6216\u8005\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u6570\u636e\u5217\u8868\u6216DataFrame\uff0c\u4f7f\u7528<code>csv.writer<\/code>\u65b9\u6cd5\u6216<code>DataFrame.to_csv()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u6587\u4ef6\u6269\u5c55\u540d\u4e3a<code>.csv<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u4ee5\u6b63\u786e\u7684\u683c\u5f0f\u4fdd\u5b58\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u683c\u5f0f\u7684\u6570\u636e\u5bfc\u51fa\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u5bfc\u51fa\u683c\u5f0f\uff0c\u5305\u62ecCSV\u3001Excel\u3001JSON\u3001SQL\u6570\u636e\u5e93\u7b49\u3002\u901a\u8fc7\u4e0d\u540c\u7684\u5e93\uff0c\u5982<code>pandas<\/code>\u53ef\u4ee5\u8f7b\u677e\u5bfc\u51fa\u5230Excel\u6216JSON\u683c\u5f0f\uff0c\u800c<code>sqlite3<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u5bfc\u51fa\u5230SQLite\u6570\u636e\u5e93\u3002\u6839\u636e\u6570\u636e\u7684\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u8fdb\u884c\u5bfc\u51fa\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5bfc\u51fa\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u5bfc\u51fa\u6570\u636e\u4e4b\u524d\uff0c\u5904\u7406\u7f3a\u5931\u503c\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528<code>pandas<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>DataFrame.fillna()<\/code>\u65b9\u6cd5\u586b\u8865\u7f3a\u5931\u503c\uff0c\u6216\u8005\u901a\u8fc7<code>DataFrame.dropna()<\/code>\u65b9\u6cd5\u5220\u9664\u7f3a\u5931\u503c\u3002\u8fd9\u6837\u7684\u5904\u7406\u53ef\u4ee5\u786e\u4fdd\u5bfc\u51fa\u7684\u6570\u636e\u96c6\u66f4\u52a0\u5b8c\u6574\uff0c\u907f\u514d\u5728\u540e\u7eed\u6570\u636e\u5206\u6790\u4e2d\u51fa\u73b0\u95ee\u9898\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001JSON\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002\u5e38\u89c1\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1150965,"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\/1150959"}],"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=1150959"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150959\/revisions"}],"predecessor-version":[{"id":1150966,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150959\/revisions\/1150966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150965"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}