Ironpython如何在其他代码行中引用计算变量

2024-03-28 22:23:08 发布

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

我在Spotfire内部与IronPython合作

我需要从范围筛选器中提取最大日期值,然后使用该值筛选汇率表

我的工作代码一直到datatable.Select语句,我需要在其中进行匹配。如果我根据“Date(2020,3,1)”(注释掉的行)进行匹配,则匹配有效,并返回正确的结果,但是我无法获得使用计算变量“newdate”代替Date(xxx)语句的语法正确。我仍在学习python,以前从未遇到过这种情况

代码如下-如有任何帮助,将不胜感激

from Spotfire.Dxp.Application.Filters import RangeFilter, ValueRange
from Spotfire.Dxp.Data.DataType import  Date
from System.Globalization import CultureInfo
parser = Date.CreateCultureSpecificFormatter(CultureInfo("en-AU"))


#get a reference to a filter as checkbox from the myDataTable script parameter
filt=Document.FilteringSchemes[Document.ActiveFilteringSelectionReference].Item[dt].Item[dt.Columns.Item["Calendar Date"]].As[RangeFilter]()

print filt.ValueRange.High

if str(filt.ValueRange.High) == "High":
    maxdate = Document.Properties["loaddate"]
else: 
    maxdate = filt.ValueRange.High

maxdate = Date.Formatter.Parse(maxdate)
print maxdate
new = str(maxdate.Year) + "," + str(maxdate.Month) + "," + str("1")
print new
Document.Properties["maxdate"] = new


from Spotfire.Dxp.Data import *
from System.Collections.Generic import List
table=Document.ActiveDataTableReference
# Expression to limit the data in a table 
rowSelection=table.Select("CALENDAR_DATE = Date('new')")
#rowSelection=table.Select("CALENDAR_DATE = Date(2020,3,1)")

# Create a cursor to the Column we wish to get the values from
cursor = DataValueCursor.CreateFormatted(table.Columns["FY_AVERAGE_EXCHANGE"])

# Create List object that will hold values
listofValues=List[str]()

# Loop through all rows, retrieve value for specific column,
# and add value into list
for  row in  table.GetRows(rowSelection.AsIndexSet(),cursor):
   rowIndex = row.Index
   value1 = cursor.CurrentValue
   listofValues.Add(value1)


for val in listofValues:
    print val

Tags: thetofromimportnewdatetabledocument
1条回答
网友
1楼 · 发布于 2024-03-28 22:23:08

我想你的变量new将打印为2020,01,01

在这一行中new是一个字符串,因此Date()无法提取日期

rowSelection=table.Select("CALENDAR_DATE = Date('new')")

您应该将new作为变量

rowSelection=table.Select("CALENDAR_DATE = Date(" +new +")")

但不确定它是否能工作,因为日期接受整数而不是字符串,所以您可能必须重新写入:

y = maxdate.Year
m= maxdate.Month 
d = 1 

rowSelection=table.Select("CALENDAR_DATE = Date("+ y + ',' + m +',' + d + ")")

或者,我会使用的方法是先构建字符串:

y = maxdate.Year
m= maxdate.Month 
d = 1 
mystring =  "CALENDAR_DATE = Date("+ str(y) + ',' + str(m) +',' + str(d) + ")"

rowSelection=table.Select(mystring) 

上面的一种方法应该是可行的,我从前面设置字符串的最后一种方法开始,因为最好不要处理整数和字符串的多次转换

如果您将此问题与示例DXP一起发布到Tibco,则答案可能会有更多帮助,因为您将使用示例DXP。但希望这能帮到你

相关问题 更多 >