I Get TypeError: cannot use a string pattern on a bytes-like object when using to_sql on dataframe python 3

可紊 提交于 2021-02-10 22:19:09

问题


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.

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.

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')

Can somebody please help?

    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 ```

回答1:


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.



来源:https://stackoverflow.com/questions/62912357/i-get-typeerror-cannot-use-a-string-pattern-on-a-bytes-like-object-when-using-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!