我得到TypeError:在数据框python 3上使用to_sql时无法在类似字节的对象上使用字符串模式 [英] I Get TypeError: cannot use a string pattern on a bytes-like object when using to_sql on dataframe python 3
问题描述
我正在尝试使用df.to_sql将数据帧写入我的sql数据库,但是却收到错误消息:TypeError:无法在类似字节的对象上使用字符串模式.我正在使用Python 3.
Hi I am trying to write a dataframe to my sql database using df.to_sql however I am getting the error message: TypeError: cannot use a string pattern on a bytes-like object. I am using Python 3.
我在驱动器上使用了一条我无法共享的路径.但是当我只想使用打开csv文件时,它可以正常工作. df = pd.read_csv(path,delimiter =';',engine ='python',low_memory = True,encoding ='utf-8-sig')
我正在使用编码项,因为否则它们在我的索引列中是一个奇怪的对象.但是,即使不使用编码,我也会遇到相同的错误.
I am using a path on my drive which I can unfortuantly not share. But it works fine when I just want to open the csv file using.
df = pd.read_csv(path, delimiter=';', engine='python', low_memory=True, encoding='utf-8-sig')
I am using the encoding item because otherwise their is a strange object at my index column. But also without using encoding I get the same error.
我还在数据框上使用了 df.dtypes
,但没有字节对象.只有int,对象和浮点数.我还尝试使用 bytes(df [x]).decode('utf-8')
来确定我的数据框中是否有类似字节的对象,但是我只能得到整数列而没有对象列.我尝试对其进行解码,然后再使用to_sql,但没有任何帮助. df.to_sql('DMM_2300',con = engine,index = False,if_exists ='append')
I have also used df.dtypes
on my dataframe but no bytes objects. Only int, objects and floats. I have also tried to figure out if I have bytes-like objects in my dataframe using bytes(df[x]).decode('utf-8')
but I only get integer columns and not object columns. Which I tried to decode and then use to_sql upon but nothing helps.
df.to_sql('DMM_2300', con=engine, index=False, if_exists='append')
有人可以帮忙吗?
method=method,
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 521, in to_sql
method=method,
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 1317, in to_sql
table.insert(chunksize, method=method)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 755, in insert
exec_insert(conn, keys, chunk_iter)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 669, in _execute_insert
conn.execute(self.table.insert(), data)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1449, in execute
params)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1584, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1680, in _execute_context
context)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\connectors\mysqldb.py", line 55, in do_executemany
rowcount = cursor.executemany(statement, parameters)
File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 189, in executemany
m = RE_INSERT_VALUES.match(query)
TypeError: cannot use a string pattern on a bytes-like object ```
推荐答案
我找到了答案.我安装了旧版本的sqlaclhemy 0.7.将其更新到最新的1.3.18版本后,该错误消失了.
I found the answer. I installed an older version of sqlaclhemy 0.7. After updating it to the latest 1.3.18 version, the error was gone.
这篇关于我得到TypeError:在数据框python 3上使用to_sql时无法在类似字节的对象上使用字符串模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!