{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import sqlite3\n",
    "# import pandas as pd\n",
    "\n",
    "# # 连接到SQLite数据库\n",
    "# conn = sqlite3.connect('database.db')\n",
    "\n",
    "# # 从数据库中读取表数据到DataFrame\n",
    "# table_name = 'your_table_name'  # 替换为实际表名\n",
    "# query = f\"SELECT * FROM {table_name}\"\n",
    "# df = pd.read_sql_query(query, conn)\n",
    "\n",
    "# 按照“本地代码”分组并导出为CSV文件\n",
    "for local_code, group in df.groupby('本地代码'):\n",
    "    # 为每个“本地代码”生成一个CSV文件，文件名使用该代码值\n",
    "    csv_filename = f\"{local_code}.csv\"\n",
    "    group.to_csv(csv_filename, index=False, encoding='utf-8-sig')\n",
    "    print(f\"数据已导出到 {csv_filename}\")\n",
    "\n",
    "# 关闭数据库连接\n",
    "conn.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 连接到SQLite数据库\n",
    "conn = sqlite3.connect(r'D:\\of_data\\database.db')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从数据库中读取表数据到DataFrame\n",
    "table_name = 'dbbardata'  # 替换为实际表名\n",
    "query = f\"SELECT * FROM {table_name}\"\n",
    "df = pd.read_sql_query(query, conn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del(df['id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "del group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已导出到 AP00_CZCE.csv\n",
      "数据已导出到 APJQ00_CZCE.csv\n",
      "数据已导出到 CF00_CZCE.csv\n",
      "数据已导出到 CFJQ00_CZCE.csv\n",
      "数据已导出到 CJ00_CZCE.csv\n",
      "数据已导出到 CJJQ00_CZCE.csv\n",
      "数据已导出到 CY00_CZCE.csv\n",
      "数据已导出到 CYJQ00_CZCE.csv\n",
      "数据已导出到 FG00_CZCE.csv\n",
      "数据已导出到 FGJQ00_CZCE.csv\n",
      "数据已导出到 IC00_CFFEX.csv\n",
      "数据已导出到 ICJQ00_CFFEX.csv\n",
      "数据已导出到 IF00_CFFEX.csv\n",
      "数据已导出到 IFJQ00_CFFEX.csv\n",
      "数据已导出到 IH00_CFFEX.csv\n",
      "数据已导出到 IHJQ00_CFFEX.csv\n",
      "数据已导出到 IM00_CFFEX.csv\n",
      "数据已导出到 IMJQ00_CFFEX.csv\n",
      "数据已导出到 JR00_CZCE.csv\n",
      "数据已导出到 JRJQ00_CZCE.csv\n",
      "数据已导出到 LR00_CZCE.csv\n",
      "数据已导出到 LRJQ00_CZCE.csv\n",
      "数据已导出到 MA00_CZCE.csv\n",
      "数据已导出到 MAJQ00_CZCE.csv\n",
      "数据已导出到 OI00_CZCE.csv\n",
      "数据已导出到 OIJQ00_CZCE.csv\n",
      "数据已导出到 PF00_CZCE.csv\n",
      "数据已导出到 PFJQ00_CZCE.csv\n",
      "数据已导出到 PK00_CZCE.csv\n",
      "数据已导出到 PKJQ00_CZCE.csv\n",
      "数据已导出到 PM00_CZCE.csv\n",
      "数据已导出到 PMJQ00_CZCE.csv\n",
      "数据已导出到 PX00_CZCE.csv\n",
      "数据已导出到 PXJQ00_CZCE.csv\n",
      "数据已导出到 RI00_CZCE.csv\n",
      "数据已导出到 RIJQ00_CZCE.csv\n",
      "数据已导出到 RM00_CZCE.csv\n",
      "数据已导出到 RMJQ00_CZCE.csv\n",
      "数据已导出到 RS00_CZCE.csv\n",
      "数据已导出到 RSJQ00_CZCE.csv\n",
      "数据已导出到 SA00_CZCE.csv\n",
      "数据已导出到 SAJQ00_CZCE.csv\n",
      "数据已导出到 SF00_CZCE.csv\n",
      "数据已导出到 SFJQ00_CZCE.csv\n",
      "数据已导出到 SH00_CZCE.csv\n",
      "数据已导出到 SHJQ00_CZCE.csv\n",
      "数据已导出到 SM00_CZCE.csv\n",
      "数据已导出到 SMJQ00_CZCE.csv\n",
      "数据已导出到 SR00_CZCE.csv\n",
      "数据已导出到 SRJQ00_CZCE.csv\n",
      "数据已导出到 T00_CFFEX.csv\n",
      "数据已导出到 TA00_CZCE.csv\n",
      "数据已导出到 TAJQ00_CZCE.csv\n",
      "数据已导出到 TF00_CFFEX.csv\n",
      "数据已导出到 TFJQ00_CFFEX.csv\n",
      "数据已导出到 TJQ00_CFFEX.csv\n",
      "数据已导出到 TL00_CFFEX.csv\n",
      "数据已导出到 TLJQ00_CFFEX.csv\n",
      "数据已导出到 TS00_CFFEX.csv\n",
      "数据已导出到 TSJQ00_CFFEX.csv\n",
      "数据已导出到 UR00_CZCE.csv\n",
      "数据已导出到 URJQ00_CZCE.csv\n",
      "数据已导出到 WH00_CZCE.csv\n",
      "数据已导出到 WHJQ00_CZCE.csv\n",
      "数据已导出到 ZC00_CZCE.csv\n",
      "数据已导出到 ZCJQ00_CZCE.csv\n",
      "数据已导出到 a00_DCE.csv\n",
      "数据已导出到 aJQ00_DCE.csv\n",
      "数据已导出到 ag00_SHFE.csv\n",
      "数据已导出到 agJQ00_SHFE.csv\n",
      "数据已导出到 al00_SHFE.csv\n",
      "数据已导出到 alJQ00_SHFE.csv\n",
      "数据已导出到 ao00_SHFE.csv\n",
      "数据已导出到 aoJQ00_SHFE.csv\n",
      "数据已导出到 au00_SHFE.csv\n",
      "数据已导出到 auJQ00_SHFE.csv\n",
      "数据已导出到 b00_DCE.csv\n",
      "数据已导出到 bJQ00_DCE.csv\n",
      "数据已导出到 bb00_DCE.csv\n",
      "数据已导出到 bbJQ00_DCE.csv\n",
      "数据已导出到 bc00_INE.csv\n",
      "数据已导出到 bcJQ00_INE.csv\n",
      "数据已导出到 br00_SHFE.csv\n",
      "数据已导出到 brJQ00_SHFE.csv\n",
      "数据已导出到 bu00_SHFE.csv\n",
      "数据已导出到 buJQ00_SHFE.csv\n",
      "数据已导出到 c00_DCE.csv\n",
      "数据已导出到 cJQ00_DCE.csv\n",
      "数据已导出到 cs00_DCE.csv\n",
      "数据已导出到 csJQ00_DCE.csv\n",
      "数据已导出到 cu00_SHFE.csv\n",
      "数据已导出到 cuJQ00_SHFE.csv\n",
      "数据已导出到 eb00_DCE.csv\n",
      "数据已导出到 ebJQ00_DCE.csv\n",
      "数据已导出到 ec00_INE.csv\n",
      "数据已导出到 ecJQ00_INE.csv\n",
      "数据已导出到 eg00_DCE.csv\n",
      "数据已导出到 egJQ00_DCE.csv\n",
      "数据已导出到 fb00_DCE.csv\n",
      "数据已导出到 fbJQ00_DCE.csv\n",
      "数据已导出到 fu00_SHFE.csv\n",
      "数据已导出到 fuJQ00_SHFE.csv\n",
      "数据已导出到 hc00_SHFE.csv\n",
      "数据已导出到 hcJQ00_SHFE.csv\n",
      "数据已导出到 i00_DCE.csv\n",
      "数据已导出到 iJQ00_DCE.csv\n",
      "数据已导出到 j00_DCE.csv\n",
      "数据已导出到 jJQ00_DCE.csv\n",
      "数据已导出到 jd00_DCE.csv\n",
      "数据已导出到 jdJQ00_DCE.csv\n",
      "数据已导出到 jm00_DCE.csv\n",
      "数据已导出到 jmJQ00_DCE.csv\n",
      "数据已导出到 l00_DCE.csv\n",
      "数据已导出到 lJQ00_DCE.csv\n",
      "数据已导出到 lc00_GFEX.csv\n",
      "数据已导出到 lcJQ00_GFEX.csv\n",
      "数据已导出到 lh00_DCE.csv\n",
      "数据已导出到 lhJQ00_DCE.csv\n",
      "数据已导出到 lu00_INE.csv\n",
      "数据已导出到 luJQ00_INE.csv\n",
      "数据已导出到 m00_DCE.csv\n",
      "数据已导出到 mJQ00_DCE.csv\n",
      "数据已导出到 ni00_SHFE.csv\n",
      "数据已导出到 niJQ00_SHFE.csv\n",
      "数据已导出到 nr00_INE.csv\n",
      "数据已导出到 nrJQ00_INE.csv\n",
      "数据已导出到 p00_DCE.csv\n",
      "数据已导出到 pJQ00_DCE.csv\n",
      "数据已导出到 pb00_SHFE.csv\n",
      "数据已导出到 pbJQ00_SHFE.csv\n",
      "数据已导出到 pg00_DCE.csv\n",
      "数据已导出到 pgJQ00_DCE.csv\n",
      "数据已导出到 pp00_DCE.csv\n",
      "数据已导出到 ppJQ00_DCE.csv\n",
      "数据已导出到 rb00_SHFE.csv\n",
      "数据已导出到 rbJQ00_SHFE.csv\n",
      "数据已导出到 rr00_DCE.csv\n",
      "数据已导出到 rrJQ00_DCE.csv\n",
      "数据已导出到 ru00_SHFE.csv\n",
      "数据已导出到 ruJQ00_SHFE.csv\n",
      "数据已导出到 sc00_INE.csv\n",
      "数据已导出到 scJQ00_INE.csv\n",
      "数据已导出到 si00_GFEX.csv\n",
      "数据已导出到 siJQ00_GFEX.csv\n",
      "数据已导出到 sn00_SHFE.csv\n",
      "数据已导出到 snJQ00_SHFE.csv\n",
      "数据已导出到 sp00_SHFE.csv\n",
      "数据已导出到 spJQ00_SHFE.csv\n",
      "数据已导出到 ss00_SHFE.csv\n",
      "数据已导出到 ssJQ00_SHFE.csv\n",
      "数据已导出到 v00_DCE.csv\n",
      "数据已导出到 vJQ00_DCE.csv\n",
      "数据已导出到 wr00_SHFE.csv\n",
      "数据已导出到 wrJQ00_SHFE.csv\n",
      "数据已导出到 y00_DCE.csv\n",
      "数据已导出到 yJQ00_DCE.csv\n",
      "数据已导出到 zn00_SHFE.csv\n",
      "数据已导出到 znJQ00_SHFE.csv\n"
     ]
    }
   ],
   "source": [
    "for local_code, group in df.groupby('symbol'):\n",
    "    # 为每个“本地代码”生成一个CSV文件，文件名使用该代码值\n",
    "    exchange = group.exchange.iloc[0]\n",
    "    csv_filename = f\"{local_code}_{exchange}.csv\"\n",
    "    group.to_csv(csv_filename, index=False, encoding='utf-8-sig')\n",
    "    print(f\"数据已导出到 {csv_filename}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "conn.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
