'numpy.float64' object has no attribute 'translate' Inserting value to Mysql in Python

前端 未结 4 759
庸人自扰
庸人自扰 2021-02-14 15:00
import dataset
db = dataset.connect(....)
table = db[...]

When I try to insert some value into Mysql table, this error occurred.

Sample Value

4条回答
  •  离开以前
    2021-02-14 15:28

    Another option, that could help it is:

    from sqlalchemy import event
    import numpy as np
    import sqlalchemy
    
    engine = sqlalchemy.create_engine(...)
    
    def add_own_encoders(conn, cursor, query, *args):
        cursor.connection.encoders[np.float64] = lambda value, encoders: float(value)
    
    event.listen(engine, "before_cursor_execute", add_own_encoders)
    

提交回复
热议问题