读取csv、txt等文件存到数据库

import pandas as pd
from sqlalchemy import create_engine
import time


data = pd.read_csv('/home/ubuntu/Desktop/measurements_new1.txt', delim_whitespace=True)  # delim_whitespace=True 空格作为分隔符,默认为False
engine = create_engine("mysql+pymysql://{}:{}@{}/{}?charset={}".format('user', 'password', '127.0.0.1:3306', 'database_name','utf8'))
con = engine.connect()


data_10 = data.iloc[0:10,:]
data_100 = data.iloc[0:100,:]
data_1000 = data.iloc[0:1000,:]
data_10000 = data.iloc[0:10000,:]
data_100000 = data.iloc[0:100000,:]

data_list = [data_10, data_100,data_1000,data_10000,data_100000]
for i in data_list:
    start = time.time()
    i.to_sql(name='table_name', con=con, if_exists='append', index=False)
    end = time.time()
    print(end-start)

你可能感兴趣的:(读取csv、txt等文件存到数据库)