我有一些excel数据文件,我想导入MySQL数据库。我用pandas(read\uexcel)创建了一个脚本,因为我有重音符号和其他特殊字符,所以我必须用utf8对pandas读取的数据进行编码。str.encode编码()(我的MySQL数据库是utf8)。问题是当我将数据导入数据库时,使用to_sql(),将所有空值(NaN,NaT,null,…)转换为字符串。。。你知道吗
我试过很多编码。我尝试用fillna()替换空值,也将它们替换为None,我还设置mysql以避免无效值(STRICT\u TRANS MODE et NO\u ZERO\u DATE for NaT)
for y in range(0,len(FICHIERS)):
DONNEES[FICHIERS[y].split('.')[0]] = pd.read_excel(os.path.join(APPLICATION_PATH,CONFIG.get('Excel','Chemin_Excel')+ FICHIERS[y] ), index_col=None)
def convertUTF(dataset):
for col in dataset:
dataset[col] = dataset[col].astype(str).str.encode(encoding='UTF-8',errors='replace')
return dataset
#converting all data to utf8
for nom, valeur in DONNEES.items():
DONNEES[nom] = convertUTF(valeur)
#insert into the db
engine = create_engine(INFOS_CONNECTION, encoding="utf-8", echo=True)
con = engine.connect()
con.execute("SET GLOBAL SQL_MODE='NO_ZERO_DATE';")
con.execute('SET FOREIGN_KEY_CHECKS=0;')
con.execute('SET NAMES utf8;')
con.execute('SET CHARACTER SET utf8;')
con.execute('SET character_set_connection=utf8;')
for k, v in DONNEES.items():
v.to_sql(con=con, name=k.lower(), if_exists='append',index=False)
这是我的数据,当我在MySQL中导入时,NaT变成0000-00-00(作为字符串)和NaN“NaN”(也作为字符串…我也试图用“null”替换null,但它仍然是字符串。。。不是真正的空sql值)。你知道吗
print(DONNEES['T_Liste******t'])
CodePersonnel PrenomEnfant NomEnfant DateNaissance DateDeces Enfant*** Sexe ClientPresent Archiver
0 ------- ------- ------- 1985-05-05 NaT NaN 1 0 0
1 ------- ------- ------- 1991-02-11 NaT NaN 2 0 0
2 ------- ------- ------- 1990-01-26 NaT NaN 2 0 0
3 ------- ------- ------- 1982-11-23 NaT NaN 2 0 0
4 ------- ------- ------- 1985-06-07 NaT NaN 1 0 0
... ... ... ... ... ...
(我把名字换成了-----保安)
目前没有回答
相关问题 更多 >
编程相关推荐