py读取txt文本内容后写入mysql表,若表不存在则创建一个表后插入
时间: 2024-11-22 10:43:45 浏览: 12
在Python中,可以使用`pandas`库结合`mysql-connector-python`库来实现这个功能。首先,你需要安装这两个库,如果还没有的话:
```bash
pip install pandas mysql-connector-python
```
然后,你可以按照以下步骤操作:
1. **读取txt文件**:
使用`pandas`的`read_csv`函数读取txt文件,假设它是逗号分隔的,如果文件不是csv格式,可能需要调整处理方式。
```python
import pandas as pd
def read_txt_and_convert_to_df(file_path):
data = pd.read_csv(file_path, delimiter='\t' or ',' or '\n', engine='python') # 根据实际情况修改分隔符
return data
```
2. **连接到MySQL数据库**:
首先导入必要的模块,并建立连接:
```python
import mysql.connector
def connect_mysql(host, user, password, db_name):
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=db_name
)
return connection
```
3. **检查并创建表**:
检查表是否存在,如果不存在就创建它:
```python
def create_table_if_not_exists(connection, table_name, column_names):
cursor = connection.cursor()
try:
cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
if not cursor.fetchone():
create_table_query = f"CREATE TABLE `{table_name}` ({', '.join(column_names)})"
cursor.execute(create_table_query)
connection.commit()
print(f"Table {table_name} created.")
except mysql.connector.Error as e:
print(f"Error creating table: {e}")
cursor.close()
```
4. **插入数据**:
确保表存在后,将数据插入:
```python
def insert_data(connection, df, table_name):
df.to_sql(table_name, con=connection, if_exists='append', index=False)
print(f"{df.shape[0]} rows inserted into {table_name}.")
```
5. **整体流程整合**:
```python
file_path = 'your_file.txt'
host, user, password, db_name = 'localhost', 'your_username', 'your_password', 'your_db'
# Read txt file
data = read_txt_and_convert_to_df(file_path)
# Connect to MySQL
connection = connect_mysql(host, user, password, db_name)
# Create the table if it doesn't exist
column_names = ['column1', 'column2', ...] # Replace with your actual column names
create_table_if_not_exists(connection, 'your_table_name', ', '.join(column_names))
# Insert data into the table
insert_data(connection, data, 'your_table_name')
# Close the connection
connection.close()
```
记得替换上述代码中的占位符(如文件路径、用户名、密码、数据库名、列名和表名)。
阅读全文