一个数据结构转换的问题

问题背景

#给定一个字典{"uid", 2243,"follow", 23,"fans", 20,"weibo", 45}
需要把上面的字典字典转换成3条数据插入数据库。
# BIZ_ID: 2243, ITEM_NAME:"follow", DEC_VALUE:23
# BIZ_ID: 2243, ITEM_NAME: "fans", DEC_VALUE: 20
# BIZ_ID: 2243, ITEM_NAME: "weibo", DEC_VALUE: 45

 

方案1

            uid = home_data_dicts.get("uid")
            follow = home_data_dicts.get("follow")
            fans = home_data_dicts.get("fans")
            weibo = home_data_dicts.get("weibo")
            self.save_home_data_dicts.setdefault("BIZ_ID", []).extend([uid, uid, uid])
            self.save_home_data_dicts.setdefault("ITEM_NAME", []).append("follow")
            self.save_home_data_dicts.setdefault("ITEM_NAME", []).append("fans")
            self.save_home_data_dicts.setdefault("ITEM_NAME", []).append("weibo")
            self.save_home_data_dicts.setdefault("DEC_VALUE", []).append(follow)
            self.save_home_data_dicts.setdefault("DEC_VALUE", []).append(fans)
            self.save_home_data_dicts.setdefault("DEC_VALUE", []).append(weibo)
            self.save_home_data_dicts.setdefault("LOG_DATE", []).extend([nowdate, nowdate, nowdate])
            self.save_home_data_dicts.setdefault("UPD_DATE", []).extend([nowdate, nowdate, nowdate])
            df=pd.DataFrame(self.save_home_data_dicts)
            pd.io.sql.to_sql(df,"sns_insight_log",con=eng,schema=self.database,if_exists="append",index=False)

  

posted @ 2018-03-07 16:55  公众号python学习开发  阅读(165)  评论(0编辑  收藏  举报