【问题标题】:spotfire ironpython : Append new row to a data-tablespotfire ironpython:将新行附加到数据表
【发布时间】:2015-06-02 01:24:16
【问题描述】:

我有一个 csv 字符串输入
string_in="country,100,color"
谁能建议我如何在 spotfire 中使用 ironpython 脚本将此输入(string_in)附加到已经存在的
数据表可视化中。
谢谢。

【问题讨论】:

    标签: ironpython spotfire


    【解决方案1】:

    您需要引入一些不同的函数来通过脚本实现这一目标。我在下面有我的脚本,但总体而言,您要做的是将输入设置为数据源,然后将该数据源中的行添加到现有数据源中。我从 TIBCO 的 thisspotfire 教程中借了很多钱,并根据需要对其进行了修改,以使我们到达您想去的地方。因此,您可能会删除一些多余的导入。

    我使用 datTab 作为脚本的输入参数作为我们想要添加行的数据表。

    from Spotfire.Dxp.Data import AddRowsSettings
    import System
    from System import DateTime
    from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
    from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
    from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
    
    #First we need to create your data with some columns. 
    #Here I have a comma separated miniature table built up with 
    #a commented option for your variable itself. \r\n used for newline
    textData = "name,values,category\r\ncountry,100,color\r\n" 
    #textData = "col1,col2,col3\r\n" + string_in + "\r\n"
    
    #Memory Stream stuff. Simply just writing our variable 
    #into a place we can access to make a data source
    stream = MemoryStream()
    writer = StreamWriter(stream)
    writer.Write(textData)
    writer.Flush()
    stream.Seek(0, SeekOrigin.Begin)
    
    #you need settings to tell the system what stuff you're importing.
    #here we define it is comma separated and what data types our columns are.
    readerSettings = TextDataReaderSettings()
    readerSettings.Separator = ","
    readerSettings.AddColumnNameRow(0)
    readerSettings.SetDataType(0, DataType.String)
    readerSettings.SetDataType(1, DataType.Integer)
    readerSettings.SetDataType(2, DataType.String)
    textDataSource = TextFileDataSource(stream,readerSettings)
    
    #We create some settings here automatically having the system match
    #column names for us between the data table and our data source.
    settings = AddRowsSettings(datTab,textDataSource)
    #And finally we add the rows from our datasource with our settings.
    datTab.AddRows(textDataSource,settings)
    

    当然,您可以使用更长的输入变量使这更复杂,循环添加多行等。您也可以使用指向文件的 URL 而不是内存流的东西来遵循相同的过程。取决于您的输入类型。

    如果您有任何问题,请告诉我。我试图评论重要部分,但如果需要,可以对特定功能进行进一步解释。

    编辑:在我添加了几次不同的记录后,请参阅下面的屏幕截图@

    【讨论】:

    • 谢谢@clesiemo3。这是最有帮助的。
    • 我有一个数据表 ALLDataTable,其中有几行被 标记。我正在尝试将选定的行复制到另一个名为 Subset 的数据表中。我使用的逻辑是将标记的行复制到 temp.txt 文件,然后导入到“子集”数据表。没有运行时错误,但是当我查看新创建的子集数据表时,有一条消息“所选数据表不包含任何列”。你能帮我解决这个问题吗?代码在这里-ideone.com/fork/PTUAo7
    • @diablo8226,您最好提出一个新问题,而不是对此发表评论。乍一看,它可能不会保存到文件中,因为您只是在写入文件但没有将其关闭。我知道在上面的示例中,我使用 MemoryStream() 而不是您的临时文件,但我认为它们在概念上是相同的。如果有的话,我会尝试将相当于 flush+seek 的 txt 文件设置为 0。我不知道在我的头顶。 tmp 文件超过内存流的任何原因?
    • 没有具体原因。我使用了内存流,它工作得很好。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    相关资源
    最近更新 更多