MySQL数据库里有个银行明细表,数据量很大,怎么根据交易卡号批量大数据导出为Excel文件并且导出的数据以相对应的卡号命名
时间: 2024-03-19 09:45:04 浏览: 154
可以使用MySQL的SELECT INTO OUTFILE语句将数据导出为CSV文件,然后使用Python的pandas库将CSV文件读入DataFrame中,并将DataFrame中对应卡号的数据写入Excel文件中,并以相应卡号命名。
以下是大致的代码示例:
```python
import pandas as pd
import pymysql
# MySQL连接配置
host = 'localhost'
port = 3306
user = 'root'
password = 'password'
database = 'mydb'
# 连接MySQL
conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
# SQL语句
sql = "SELECT * FROM bank_detail WHERE card_number IN ('card1', 'card2', 'card3')"
# 导出CSV文件
csv_file = 'data.csv'
cursor = conn.cursor()
cursor.execute(sql)
with open(csv_file, 'w') as f:
writer = csv.writer(f)
writer.writerow([i[0] for i in cursor.description]) # 列名
writer.writerows(cursor.fetchall()) # 数据
# 读取CSV文件
df = pd.read_csv(csv_file)
# 将数据写入Excel文件中
excel_file = 'data.xlsx'
with pd.ExcelWriter(excel_file) as writer:
for card_number in df['card_number'].unique():
df[df['card_number'] == card_number].to_excel(writer, sheet_name=card_number, index=False)
```
注意,以上代码仅供参考,具体实现还需要根据实际情况进行修改。
阅读全文