我需要使用IronPython从Spotfire的临时文件中读取大量数据。
首先,我使用exported text()方法将我的Tibco数据表导出到一个临时文件中:
#Temp file for storing the TablePlot data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
#Export TablePlot data to the temp file
tp = tablePlotViz.As[TablePlot]()
writer = StreamWriter(tempFilename)
tp.ExportText(writer)然后,使用open()方法打开临时文件。
f = open(tempFilename)现在,当我开始从打开的文件中读取数据并将其写回字符串变量时,这会花费太多时间。我的Spotfire屏幕停止工作了。
有人知道这是怎么回事吗?
我的数据表是8MB大小。
代码为:
from Spotfire.Dxp.Application.Visuals import TablePlot, HtmlTextArea
import clr
import sys
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, FileStream, FileMode,Path, File
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
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 Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
from System import Array
from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Application.Visuals import TablePlot
from System.IO import Path, StreamWriter
from System.Text import StringBuilder
#Temp file for storing the TablePlot data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
#Export TablePlot data to the temp file
tp = tablePlotViz.As[TablePlot]()
writer = StreamWriter(tempFilename)
tp.ExportText(writer)
#Build the table
sb = StringBuilder()
#Open the temp file for reading
f = open(tempFilename)
#build the html table
html = " <TABLE id='table' style='display:none;'>\n"
html += "<THEAD>"
html += " <TR><TH>"
html += " </TH><TH>".join(f.readline().split("\t")).strip()
html += " </TH></TR>"
html += "</THEAD>\n"
html += "<TBODY>\n"
for line in f:
html += "<TR><TD>"
html += "</TD><TD>".join(line.split("\t")).strip()
html += "</TD></TR>\n"
#Assigned the all HTML data in the text area
print html该代码可以很好地处理短数据。
发布于 2017-02-20 00:07:35
如果我没弄错的话,代码背后的意图是将Table Plot可视化数据读取到一个字符串中,以便在HTML文本区域中进一步使用。有一种替代方法可以做到这一点,而无需将数据写入临时文件。我们可以使用内存流导出数据,并将导出的文本转换为字符串以供进一步重用。示例代码可以从here参考。
https://stackoverflow.com/questions/42225436
复制相似问题