TabPy TypeError %d format a number is required, not str, followed by no return 值

TabPy TypeError %d format a number is required, not str, followed by no return value

我刚开始在 10.13.2 上使用 TabPy。我使用了 conda pymysql 包(pymysql 和 PyMySQL-0.8.0-py2.7.egg-info)并将它们放在 anaconda 的站点包中,这样 Tableau 就会能够连接到数据库,检索数据集并保存在计算字段中。

我最初尝试 mysql.connector,就像我在 PyCharm 和 CLI 中所做的那样,但是 TabPy 使用 anaconda,它没有 mysql.[=16 的站点包=]

无论如何,它最初连接到 TabPy 服务器,returns:

An error occurred while communicating with the Predictive Service.

然而,接下来的两行紧随其后:

Error processing script
TypeError : %d format: a number is required, not str

我已经对上述错误进行了一些挖掘,并且我尝试的所有操作都产生了相同的错误。我找到了解决方案,但最后出现另一个错误。

SCRIPT_REAL("
import pymysql

db = pymysql.connect(
                     host='localhost',
                     port='9999',
                     user='usern',
                     passwd='passw',
                     db='someDb'
                     )

cur = db.cursor()

t1 = int(0)

t2 = datetime.datetime(2018, 2, 1)

sqlStr = 'select distinct APPL_ID, APPL_SUBMIT_DT from APPL_APP where APPL_ACTIVE_FLAG > %d and APPL_SUBMIT_DT >= %d' % (t1, t2)

cur.execute()

for row in cur.fetchall():
    print row

db.close()
",
COUNT([Appl Id])
)

我不明白为什么脚本会返回这样的错误,直到我 运行 它在 PyCharm 中。我的端口号需要是数字而不是字符串。

import pymysql

db = pymysql.connect(
                     host='localhost',
                     port=9999,
                     user='usern',
                     passwd='passw',
                     db='someDb'
                     )

cur = db.cursor()

t1 = int(0)

t2 = (2018-02-01)

sqlStr = 'select distinct APPL_ID, APPL_SUBMIT_DT from APPL_APP where APPL_ACTIVE_FLAG > %d and APPL_SUBMIT_DT >= %d' % (t1, t2)

cur.execute(sqlStr)

for row in cur.fetchall():
    print row

db.close()

当然,虽然我可以看到所有数据都通过 TabPy 服务器返回到我的终端,但它以以下内容结束:

(3957423, datetime.datetime(2018, 2, 27, 15, 30, 16))
(3957424, datetime.datetime(2018, 2, 27, 15, 31))
(3957425, datetime.datetime(2018, 2, 27, 15, 31, 4))
(3957426, datetime.datetime(2018, 2, 27, 15, 31, 55))
(3957428, datetime.datetime(2018, 2, 27, 15, 32, 17))
(3957429, datetime.datetime(2018, 2, 27, 15, 32, 18))
None
ERROR:__main__:{"info": null, "ERROR": "Error running script. No return value"}

这怎么可能?那里明明有数据。

为了让您的脚本在 Tableau 中运行,您需要使用 python 命令 return something,其中 something 是一个包含适当 return 类型的列表。否则,这些值可能存在于 python 中,但 Tableau 看不到它们。

在这种情况下,您需要使用如下代码构建列表:

ReturnValues=[]
for row in cur.fetchall():
   ReturnValues.append(row)
return ReturnValues

然后完整的行列表将发送回 Tableau。不过,您可能仍然遇到问题,因为 Tableau 需要一个特定大小的列表,该列表与发送到 python 的输入列表相匹配。你没有这样的输入可能会出问题