AttributeError: 'numpy.int64' object has no attribute 'translate'

在用pymysql將dataframe數據逐行寫入數據庫時,報AttributeError: ‘numpy.int64’ object has no attribute 'translate’這個錯誤。

報錯原因:將dataframe該行中是數值型數據的字段的類型打印出來,發現該字段是numpy.int64類型,但是在數據庫中對應要寫入的字段是int型的。

解決方法:在該行中numpy.int64類型的字段類型轉爲int型的。例如像下面這樣,將line[“cluster_category_code”]轉化爲int型即可。

sql = "UPDATE cs_cluster_keywords_info SET after_filter_keywords = %s WHERE keyword_id = %s and cluster_category_code = %s"
        print(type(line["cluster_category_code"]))
        cursor.execute(sql, [line["after_filter_keywords"], keywordId,int(line["cluster_category_code"])])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章