Skip to main content
 首页 » 编程设计

python之TabPy Python 脚本 "Unterminated Date"

2024年11月24日11落叶无声

我正在尝试使用 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 
) 

错误信息:

我的 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 

全屏:

我想在画面中显示的预期结果:

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

请您参考如下方法:

您看到的错误是由 Tableau Calculated 字段中的 python 注释 # 符号引起的。

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

如果您删除 Tableau Calculated 字段中的注释,它应该可以正确编译。