我正在尝试将字典中的数据插入数据库。我想遍历值并根据数据类型对它们进行相应的格式化。这是我正在使用的代码的片段:def _db_inserts(dbinfo): try: rows = dbinfo['datarows'] for row in rows: field_names = ",".join(["'{0}'".format(x) for x in row.keys()]) value_list = row.values() for pos, value in enumerate(value_list): if isinstance(value, str): value_list[pos] = "'{0}'".format(value) elif isinstance(value, datetime): value_list[pos] = "'{0}'".format(value.strftime('%Y-%m-%d')) values = ",".join(value_list) sql = "INSERT INTO table_foobar ({0}) VALUES ({1})".format(field_names, values) except Exception as e: print 'BARFED with msg:',e当我使用一些示例数据运行算法(见下文)时,出现错误:TypeError:序列项0:预期的字符串,找到的int出现上述错误的value_list数据示例如下:value_list = [377, -99999, -99999, 'f', -99999, -99999, -99999, 1108.0999999999999, 0, 'f', -99999, 0, 'f', -99999, 'f', -99999, 1108.0999999999999, -99999, 'f', -99999, 'f', -99999, 'f', 'f', 0, 1108.0999999999999, -99999, -99999, 'f', 'f', 'f', -99999, 'f', '1984-04-02', -99999, 'f', -99999, 'f', 1108.0999999999999] 我究竟做错了什么?
3 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
更换
values = ",".join(value_list)
与
values = ','.join([str(i) for i in value_list])
要么
values = ','.join(str(value_list)[1:-1])
添加回答
举报
0/150
提交
取消