美文网首页
python导入csv大文件

python导入csv大文件

作者: xueyueshuai | 来源:发表于2024-07-09 13:22 被阅读0次
pip install pandas sqlalchemy pymysql
import pandas as pd
from sqlalchemy import create_engine

# MySQL数据库连接信息
username = 'your_username'
password = 'your_password'
host = 'your_host'
database = 'your_database'
table_name = 'your_table'

# 创建数据库连接
engine = create_engine(f'mysql+pymysql://{username}:{password}@{host}/{database}')

# 大文件的分块大小(根据内存情况调整)
chunk_size = 10000

# 读取CSV文件并分块导入
csv_file = 'path_to_your_large_csv_file.csv'
for chunk in pd.read_csv(csv_file, chunksize=chunk_size):
    chunk.to_sql(name=table_name, con=engine, if_exists='append', index=False)
    print(f'Inserted chunk of size {len(chunk)}')

print("All chunks inserted successfully.")

相关文章

网友评论

      本文标题:python导入csv大文件

      本文链接:https://www.haomeiwen.com/subject/unxzcjtx.html