import pandas as pd from sqlalchemy import create_engine path ='F:/海量数据实训/data_new.csv' data = pd.read_csv(path) # 连接数据库 engine = create_engine('mysql+pymysql://root:123456@localhost:3306/Mass?charset=utf8') # 获取csv文件的表头 columns = list(data.columns) # 语句 create_table = f " create table data_new({', '.join([f'{column} varchar(255)' for column in columns])})" # 执行语句 with engine.connect() as connection: connection.execute(create_table) # 将数据写入数据库中的表中 data.to_sql('data_new', con=engine, if_exists='append', index=False)
时间: 2024-03-31 14:35:07 浏览: 19
这段代码是将一个名为 data_new.csv 的 CSV 文件导入到名为 Mass 的 MySQL 数据库中。首先,使用 Pandas 库中的 read_csv() 函数将 CSV 文件读入内存。然后,使用 SQLAlchemy 库中的 create_engine() 函数连接 MySQL 数据库。接着,获取 CSV 文件的表头,构造创建表的 SQL 语句,使用 execute() 方法在 MySQL 数据库中创建表。最后,使用 to_sql() 方法将数据写入到 MySQL 数据库的 data_new 表中。如果该表已存在,则将数据追加到该表中。如果该表不存在,则新建该表。
相关问题
优化代码:import pandas as pd from sqlalchemy import create_engine path ='F:/海量数据实训/data_new.csv' data = pd.read_csv(path) # 连接数据库 engine = create_engine('mysql+pymysql://root:123456@localhost:3306/Mass?charset=utf8') # 获取csv文件的表头 columns = list(data.columns) # 语句 create_table = f " create table data_new({', '.join([f'{column} varchar(255)' for column in columns])})" # 执行语句 with engine.connect() as connection: connection.execute(create_table) # 将数据写入数据库中的表中 data.to_sql('data_new', con=engine, if_exists='append', index=False)
这段代码看起来已经很简洁了,但是可以进行进一步的优化。可以使用 try-except-finally 结构来确保连接关闭,避免因为程序异常退出而导致连接未关闭的情况。同时,可以使用 with 语句来自动关闭文件和连接等资源,增加代码的可读性和健壮性。下面是优化后的代码:
```
import pandas as pd
from sqlalchemy import create_engine
path = 'F:/海量数据实训/data_new.csv'
data = pd.read_csv(path)
# 连接数据库
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/Mass?charset=utf8')
# 获取csv文件的表头
columns = list(data.columns)
# 构造创建表的 SQL 语句
create_table = f"CREATE TABLE data_new ({', '.join([f'{column} varchar(255)' for column in columns])})"
# 执行语句
try:
with engine.connect() as connection:
connection.execute(create_table)
data.to_sql('data_new', con=engine, if_exists='append', index=False)
except Exception as e:
print(e)
finally:
engine.dispose()
```
优化后的代码使用 with 语句来自动关闭连接,避免了因程序异常退出而导致连接未关闭的情况。同时,使用 try-except-finally 结构来确保程序的健壮性,如果出现异常,会打印异常信息。最后,使用 engine.dispose() 方法来关闭连接。
import pandas as pd salaries = pd.read_csv('data/salaries.csv', index_col=0)
这段代码使用了Python中的pandas库,将名为'salaries.csv'的文件读入到一个名为'salaries'的DataFrame中,并将第一列作为索引列。其中,'data/salaries.csv'是文件的相对路径,可以根据实际情况进行修改。这个DataFrame可以用于数据分析和处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)