我有一个程序从数据库表中逐行读取(使用pyodbc),然后执行以下操作:
目的:使用python 3 API在tableau中创建.hyper提取主要缺点:插入….
我们不知道哪个班级 newrow 。我会用的 RowClass 该标识符。
newrow
RowClass
(喜欢 RowClass = newrow.__class__ )
RowClass = newrow.__class__
我们的想法是构建一个字典,其中整数作为键和函数调用为值,并使用默认值查找函数 setDouble
setDouble
在启动时构建一次(否则,它在性能方面不那么有趣),这解释了我没有使用 newrow 和对象表示法,需要在每个新的字典重建字典 newrow 值。
calldict = { 15 : RowClass.setCharString, 7 : RowClass.setInteger, 11 : RowClass.setBoolean, 12 : lambda self,i,r : RowClass(self,i,r.year,r.month,r.day) # special case to match 2 arguments # same principle for others, of course }
你的循环添加了第一个函数查找,然后是函数调用:
while row is not None: for i in range(0,colCount): if row[i] is not None: # look up the function function_to_call = calldict.get(i,RowClass.setDouble) # call the function, passing the object as first argument: function_to_call(newrow,i,row[i])