TabPy Python脚本“未终止日期”

2024-09-30 16:41:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Tableau计算字段来使用我的python脚本。 我的python脚本查询数据库。我目前在Spyder中使用它。你知道吗

目前我得到了Unterminated Date错误。你知道吗

以下几行用红色下划线

#Remove the list comma
bookList = bookList[:-1]

sql = sql.format ("'" + startDate + "'", "'" + endDate +"'", "'" + nodeNames +"'")

print (sql)

df_Cs01 = pd.read_sql(sql,con)

con.close()

return df_Cs01
)

错误消息:

enter image description here

我的python脚本:

import pandas as pd
import pyodbc, os 
import datetime



def GetData (startDate, endDate, nodeNames, server='server'):
    con = pyodbc.connect(r'DSN='+server,autocommit=True)    

    #query removed for simplicity.
    sql = """  e (R.asOfDate >= {0} and R.asOfDate <= {1})
        and R.node = {2}  """

    bookList = ""

    print (nodeNames)
    #loop through the nodeNames
    for nodeName in nodeNames:
        bookList = bookList + "'" + nodeName + "',"


    #Remove the list comma
    bookList = bookList[:-1]

    sql = sql.format ("'" + startDate + "'", "'" + endDate +"'", "'" + nodeNames +"'")

    print (sql)

    df_Cs01 = pd.read_sql(sql,con)

    con.close()

    return df_Cs01

全屏显示:

enter image description here

我希望在表中显示的预期结果:

+------------+-------+-----------+
|   Date     | Node  |    sum    |
+------------+-------+-----------+
| 04/02/2019 | Stack | -2.90E+06 |
| 05/02/2019 | Stack | -2.90E+06 |
+------------+-------+-----------+

Tags: theimport脚本dfsqldateservercon
1条回答
网友
1楼 · 发布于 2024-09-30 16:41:18

您看到的错误是由Tableau计算字段中的python注释符号引起的。你知道吗

Tableau将#符号视为显式声明日期的方法。下面是一个可能导致您看到的“未终止日期”错误的示例(请注意日期后缺少的#符号):

enter image description here

如果删除Tableau计算字段中的注释,它应该可以正确编译。你知道吗

相关问题 更多 >