使用 Python 实现读取以分号分隔的.csv 文件的最后两行数据,不读取其他行数据。最后两行数据有30个字段,分号后无数据的字段应显示为空。将所得数据导入 MySQL 数据库并创建表格
时间: 2024-10-12 07:04:26 浏览: 19
在Python中,我们可以使用pandas库轻松处理CSV文件,然后使用mysql-connector-python模块将数据插入MySQL数据库。以下是步骤:
首先,安装必要的库:
```bash
pip install pandas mysql-connector-python
```
接着,编写Python脚本来完成任务:
```python
import pandas as pd
import mysql.connector
# 读取最后一行数据
def read_last_two_rows(filename):
df = pd.read_csv(filename, sep=';', header=None, skiprows=len(df) - 2, engine='python')
return df.values.tolist() # 将数据转换为列表,便于插入数据库
# 连接到MySQL数据库
def connect_mysql(host, user, password, database):
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
return connection
# 创建表并插入数据
def create_table_and_insert_data(connection, table_name, fields, data):
cursor = connection.cursor()
# 创建表
create_table_query = f"CREATE TABLE IF NOT EXISTS `{table_name}` ({', '.join(fields)})"
cursor.execute(create_table_query)
# 如果有空值字段,可以使用%()s占位符防止SQL注入,这里简化处理
insert_query = f"INSERT INTO `{table_name}` VALUES (%s, %s, ...)"
for row in data:
placeholders = ', '.join(['%s'] * len(row))
query = insert_query.replace('...', placeholders)
cursor.execute(query, tuple(row))
# 提交事务并关闭连接
connection.commit()
cursor.close()
connection.close()
# 示例函数调用
def main():
filename = 'yourfile.csv'
host, user, password, database = 'localhost', 'yourusername', 'yourpassword', 'yourdatabase'
fields = "field1 VARCHAR(255), field2 VARCHAR(255), ..., field30 VARCHAR(255)" # 根据实际CSV字段替换
data = read_last_two_rows(filename)
connection = connect_mysql(host, user, password, database)
create_table_and_insert_data(connection, 'yourtablename', fields.split(','), data)
if __name__ == "__main__":
main()
```
阅读全文