如何通过python在Spotfire中动态创建数据表

时间:2014-08-27 17:04:35

标签: ironpython spotfire

我需要迭代每一行,将项添加到字典中,进行一些排序,然后将结果吐出到我需要通过脚本动态创建的数据表中。

http://spotfirecommunity.tibco.com/community/blogs/tips/archive/2011/03/06/displaying-cross-table-data-as-a-new-data-table.aspx

我无法再访问此文章,是否有人有代码可以执行与此类似的操作?

由于

2 个答案:

答案 0 :(得分:8)

在一些帮助下解决了这个问题。

您必须创建.Net DataSet和DataTable,在内存中添加行,处理表并从数据创建制表符分隔的字符串,然后使用Stream创建Spotfire TextDataSource ...然后您可以创建数据Spotfire中的表使用内存文本数据源(而不是物理文本文件)。

此代码中还有其他与我试图解决的问题相关的内容,但它可能对其他人有用。

import clr
clr.AddReference('System.Data')
import System
from System.Data import DataSet, DataTable, XmlReadMode
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from System.Threading import Thread
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import RowSelection
from Spotfire.Dxp.Data import DataValueCursor
from Spotfire.Dxp.Data import DataSelection
from Spotfire.Dxp.Data import DataPropertyClass
from Spotfire.Dxp.Data import Import
from System import DateTime
from System import DateTime, TimeSpan, DayOfWeek
from datetime import date
from System.Net import HttpWebRequest
import time
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings



rowCount = Document.Data.Tables['Registrations'].RowCount
rowsToInclude = IndexSet(rowCount,True)
myList = []


#Get a cursor to the two columns we want to use. cursor1 is for the key column and cursor2 is for the column selected by the user input
registration = DataValueCursor.CreateFormatted(Document.Data.Tables['Registrations'].Columns["Registration ID"])
date1 = DataValueCursor.Create[DateTime](Document.Data.Tables['Registrations'].Columns["Date 1"])
date2 = DataValueCursor.Create[DateTime](Document.Data.Tables['Registrations'].Columns["Date 2"])
date3 = DataValueCursor.Create[DateTime](Document.Data.Tables['Registrations'].Columns["Date 3"])
date4 = DataValueCursor.Create[DateTime](Document.Data.Tables['Registrations'].Columns["Date 4"])
date5 = DataValueCursor.Create[DateTime](Document.Data.Tables['Registrations'].Columns["Date 5"])

#get users logon name not prid
#print Thread.CurrentPrincipal.Identity.Name

dataSet = DataSet()
dataTable = DataTable("pattern")
dataTable.Columns.Add("Registration ID", System.String)
dataTable.Columns.Add("Date 1", System.DateTime)
dataTable.Columns.Add("Date 2", System.DateTime)
dataTable.Columns.Add("Date 3", System.DateTime)
dataTable.Columns.Add("Date 4", System.DateTime)
dataTable.Columns.Add("Date 5", System.DateTime)
dataTable.Columns.Add("Sequence", System.String)
dataSet.Tables.Add(dataTable)

for  row in  Document.Data.Tables['Registrations'].GetRows(rowsToInclude,registration, date1,date2,date3, date4, date5):
   dates=dict()
   reg = registration.CurrentValue
   value1 = date1.CurrentValue  
   value2 = date2.CurrentValue 
   value3 = date3.CurrentValue
   value4 = date4.CurrentValue
   value5 = date5.CurrentValue  
   dates['date1'] = value1
   dates['date2'] = value2
   dates['date3'] = value3
   dates['date4'] = value4
   dates['date5'] = value5

   sortedDates = sorted(dates.values())

   #print sorted(dates.values())

   pattern = ""
   for key, value in sorted(dates.iteritems(), key=lambda (k,v): (v,k)):   
                if str(value) <> '01/01/0001 00:00:00':
                    print 'Found a zero date'
                    if key == 'date1':
                                pattern = pattern + 'd1'
                                print 'Found a date 1'
                    if key == 'date2':
                                pattern = pattern + 'd2'
                                print 'Found a date 2'
                    if key == 'date3':
                                pattern = pattern + 'd3'
                                print 'Found a date 3'
                    if key == 'date4':
                                pattern = pattern + 'd4'
                                print 'Found a date 4'
                    if key == 'date5':
                                pattern = pattern + 'd5'
                                print 'Found a date 5'


   dt = dataTable.NewRow()
   dt["Registration ID"] = reg
   dt["Date 1"] = value1
   dt["Date 2"] = value2
   dt["Date 3"] = value3
   dt["Date 4"] = value4
   dt["Date 5"] = value5
   dt["Sequence"] = pattern  
   dataTable.Rows.Add(dt)


textData = "Registration ID\tDate 1\tDate 2\tDate 3\tDate 4\tDate 5\tSequence\r\n"
for row in dataTable.Rows:
                textData +=  row["Registration ID"] + "\t" + str(row["Date 1"]) + "\t" + str(row["Date 2"]) + "\t" + str(row["Date 3"]) + "\t" + str(row["Date 4"]) + "\t" + str(row["Date 5"]) + "\t" + row["Sequence"] + "\r\n"



print textData

stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

readerSettings = TextDataReaderSettings()
readerSettings.Separator = "\t"
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.Date)
readerSettings.SetDataType(2, DataType.Date)
readerSettings.SetDataType(3, DataType.Date)
readerSettings.SetDataType(4, DataType.Date)
readerSettings.SetDataType(5, DataType.String)

dSource = TextFileDataSource(stream, readerSettings)

if Document.Data.Tables.Contains("Sequence"):
                Document.Data.Tables["Sequence"].ReplaceData(dSource)
else:
                newTable = Document.Data.Tables.Add("Sequence", dSource)
                tableSettings = DataTableSaveSettings (newTable, False, False)
                Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)

答案 1 :(得分:0)

而不是

for row in dataTable.Rows:
 textData +=  row["Registration ID"] + "\t" + str(row["Date 1"]) + "\t" + str(row["Date 2"]) + "\t" + str(row["Date 3"]) + "\t" + str(row["Date 4"]) + "\t" + str(row["Date 5"]) + "\t" + row["Sequence"] + "\r\n"

我用过

output = cStringIO.StringIO()
for row in dataTable.Rows:
  output.write(row["Registration ID"] + "\t" + str(row["Date 1"]) + "\t" +             str(row["Date 2"]) + "\t" + str(row["Date 3"]) + "\t" + str(row["Date 4"]) + "\t" + str(row["Date 5"]) + "\t" + row["Sequence"] + "\r\n")

性能大幅提升。