天天看點

【疑難雜症】資料存入Mysql中顯示:AttributeError: ‘xxx‘ object has no attribute ‘translate‘ 問題解決

AttributeError: 'xxx' object has no attribute 'translate' 問題解決

  • ​​問題​​

問題

在使用sqlalchemy将DataFrame資料存放到資料庫時候,運作代碼後輸出結果如下:

【疑難雜症】資料存入Mysql中顯示:AttributeError: ‘xxx‘ object has no attribute ‘translate‘ 問題解決

解決的方式也比較簡單,就是把DataFrame所有的字段類型都變成字元串資料類型後再進行儲存即可,核心代碼如下

for i in range(len(df.columns)):
    df.iloc[:,i] = df.iloc[:,i].astype(str)      

運作後再次執行資料儲存的操作後就沒有問題了,輸出結果如下。

【疑難雜症】資料存入Mysql中顯示:AttributeError: ‘xxx‘ object has no attribute ‘translate‘ 問題解決