使用IronPython脚本将数据表从Spotfire导出到CSV

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (157)

我有一个IronPython脚本,用于从Spotfire项目中导出所有数据表。

目前它完美无缺。它遍历所有数据表并将它们导出为“.xlsx”。现在我需要将文件导出为“.csv”,我认为这就像将“.xlsx”更改为“.csv”一样简单。

这个脚本仍然导出文件,将它们全部命名为.csv,但文件中的内容是.xlsx,我不知道如何或为什么。代码只是更改文件扩展名但不将文件转换为csv。

这是我目前使用的代码:

我已经在底部发布了完整的代码,我认为代码与我在顶部单独的代码块中的问题相关。

if(dialogResult == DialogResult.Yes):
    for d in tableList: #cycles through the table list elements defined above
        writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter)
        table =  Document.Data.Tables[d[0]] #d[0] is the Data Table name in the Spotfire project (defined above)
        filtered = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet() #OR pass the filter
        stream = File.OpenWrite(savePath+'\\'+ d[1] +".csv") #d[1] is the Excel alias name. You could also use d.Name to export with the Data Table name
        names = []
        for col in table.Columns: 
            names.append(col.Name)
        writer.Write(stream, table, filtered, names)
        stream.Close()

我认为它可能与ExcelXlsDataWriter有关。我也尝试使用ExcelXlsxDataWriter。我可以使用csv编写器吗?我相信csv和txt文件有不同的编写器。

任何帮助表示赞赏。

完整脚本如下所示:

import System
import clr
import sys

clr.AddReference("System.Windows.Forms")
from sys import exit
from System.Windows.Forms import FolderBrowserDialog, MessageBox, MessageBoxButtons, DialogResult
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File, FileStream, FileMode

#This is a list of Data Tables and their Excel file names. You can see each referenced below as d[0] and d[1] respectively.
tableList = [
            ["TestTable1"],
            ["TestTable2"],
            ]

#imports the location of the file so that there is a default place to put the exports.
from Spotfire.Dxp.Application import DocumentMetadata
dmd = Application.DocumentMetadata #Get MetaData
path = str(dmd.LoadedFromFileName) #Get Path
savePath = '\\'.join(path.split('\\')[0:-1]) + "\\DataExports\\"

dialogResult = MessageBox.Show("The files will be save to "+savePath
                +". Do you want to change location?"
                , "Select the save location", MessageBoxButtons.YesNo)
if(dialogResult == DialogResult.Yes):
    # GETS THE FILE PATH FROM THE USER THROUGH A FILE DIALOG instead of using the file location
    SaveFile = FolderBrowserDialog()
    SaveFile.ShowDialog()
    savePath = SaveFile.SelectedPath

#message making sure that the user wants to exporthe files.
dialogResult = MessageBox.Show("Export Files."
                                +"  Export Files","Are you sure?", MessageBoxButtons.YesNo)
if(dialogResult == DialogResult.Yes):
    for d in tableList: #cycles through the table list elements defined above
        writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter)
        table =  Document.Data.Tables[d[0]] #d[0] is the Data Table name in the Spotfire project (defined above)
        filtered = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet() #OR pass the filter
        stream = File.OpenWrite(savePath+'\\'+ d[1] +".csv") #d[1] is the Excel alias name. You could also use d.Name to export with the Data Table name
        names = []
        for col in table.Columns: 
            names.append(col.Name)
        writer.Write(stream, table, filtered, names)
        stream.Close()

#if the user doesn't want to export then he just gets a message
else:
    dialogResult = MessageBox.Show("ok.")
提问于
用户回答回答于

出于某种原因,Spotfire Iron Python实现不支持Python中实现的csv包。

我发现你的实现的解决方法是使用StdfDataWriter而不是ExcelXsDataWriter。STDF数据格式是Spotfire文本数据格式。Spotfire中的DataWriter类仅支持Excel和STDF(请参见此处),STDF最接近CSV。

from System.IO import File
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers

writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.StdfDataWriter)
table = Document.Data.Tables['DropDownSelectors']
filtered = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet()
stream = File.OpenWrite("C:\Users\KLM68651\Documents\dropdownexport.stdf")
names =[]
for col in table.Columns:
    names.append(col.Name)
writer.Write(stream, table, filtered, names)
stream.Close()

希望这可以帮助

扫码关注云+社区

领取腾讯云代金券