for i,j in zip(table_name,file_name): df = pd.read_excel(i) df.to_sql(j, engine,if_exists='replace')如何将字符集改为uft8mb4
时间: 2023-06-25 18:04:42 浏览: 51
可以在读取 Excel 文件时,指定字符集为 UTF-8,例如:
```
df = pd.read_excel(i, encoding='utf-8')
```
然后在将 DataFrame 写入 MySQL 数据库时,指定字符集为 utf8mb4,可以在 to_sql() 函数中增加参数 charset 和 dtype,例如:
```
df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4', dtype={
'column_name': sqlalchemy.types.VARCHAR(length=255, collation='utf8mb4_unicode_ci')
})
```
其中,'column_name' 是数据库表中的列名,length 是该列的长度,collation 是排序规则。这样就可以将字符集改为 utf8mb4 了。
相关问题
调试一下这段代码import pandas as pd import sqlalchemy from sqlalchemy import create_engine engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang') # 数据存入数据库 table_name = ['景点信息-分类后.xls','用户景点行为.xls','游记信息-分类后.xls','用户游记行为.xls','所有关系.xls'] file_name = ['scenery','scenery_action','artical','artical_action','relationship'] for i,j in zip(table_name,file_name): # df = pd.read_excel(i) # df.to_sql(j, engine,if_exists='replace') df = pd.read_excel(i, encoding='utf-8') df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4', dtype={'column_name': sqlalchemy.types.VARCHAR(length=255, collation='utf8mb4_unicode_ci')})
这段代码用于将多个 Excel 文件中的数据存入 MySQL 数据库中,并且指定表名。其中,循环中的 `zip(table_name,file_name)` 函数用于将名和表名一一对应。可以按照以下方式进行调试:
1. 确认数据库连接信息是否正确。可以尝试直接连接数据库,看是否能够成功连接,例如:
```
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang')
conn = engine.connect()
```
2. 确认需要读取的 Excel 文件路径是否正确。可以尝试使用 `os.path.exists()` 函数检查文件是否存在,例如:
```
import os
for i in table_name:
if not os.path.exists(i):
print(f"File {i} does not exist!")
```
3. 确认读取 Excel 文件时的编码是否正确。可以尝试使用 `pd.read_excel()` 函数时指定不同的编码,例如:
```
df = pd.read_excel(i, encoding='utf-8')
```
4. 确认存入数据库时的参数是否正确。可以尝试使用不同的参数,例如:
```
df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4', dtype={'column_name': sqlalchemy.types.String(length=255)})
```
通过以上方法进行调试,可以找到代码中的问题并解决。
这段代码import pandas as pd import sqlalchemy from sqlalchemy import create_engine engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang') #engine = create_engine('mysql+pymysql://user:password@host:port/database?charset=utf8mb4') # 数据存入数据库 table_name = ['景点信息-分类后.xls','用户景点行为.xls','游记信息-分类后.xls','用户游记行为.xls','所有关系.xls'] file_name = ['scenery','scenery_action','artical','artical_action','relationship'] for i,j in zip(table_name,file_name): # df = pd.read_excel(i) # df.to_sql(j, engine,if_exists='replace') df = pd.read_excel(i, encoding='utf-8') df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4',dtype={'column_name': sqlalchemy.types.VARCHAR(length=255, collation='utf8mb4_unicode_ci')})报了这个错误TypeError: to_sql() got an unexpected keyword argument 'charset'如何解决
这个错误是因为 `to_sql()` 函数不支持 `charset` 这个参数,它应该被替换为 `dtype` 参数中的 `collation` 属性。因此,你需要将代码中的 `charset` 参数替换为 `collation` 属性。
修改后的代码如下:
```
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang')
# 数据存入数据库
table_name = ['景点信息-分类后.xls', '用户景点行为.xls', '游记信息-分类后.xls', '用户游记行为.xls', '所有关系.xls']
file_name = ['scenery', 'scenery_action', 'artical', 'artical_action', 'relationship']
for i, j in zip(table_name, file_name):
df = pd.read_excel(i, encoding='utf-8')
df.to_sql(j, engine, if_exists='replace', index=False, dtype={'column_name': sqlalchemy.types.VARCHAR(length=255, collation='utf8mb4_unicode_ci')})
```
这样就可以成功地将数据存入 MySQL 数据库了。
相关推荐
![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)
path = 'D:/SMT本部出勤' excel_dir = path excel_paths = [os.path.join(excel_dir, f) for f in os.listdir(excel_dir) if f.endswith('.xls')] dfs = [] for excel_path in excel_paths: df = pd.read_excel(excel_path, engine='openpyxl') df = df.replace({'<a href=': '', '
': ''}, regex=True) # 去除超链接标签 dfs.append(df) merged_df = pd.concat(dfs) output_path = r"D:\SMT本部出勤匯總\熱資料.xlsx" merged_df.to_excel(output_path, index=False),zipfile.BadZipFile: File is not a zip file報這個![](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)