2015-08-27 257 views
1

我一直在努力与我希望成为一个简单的问题。我想将Spotfire的值(String/Real/Stringlist)输出到我电脑上的文本文件中。我的最终解决方案需要列出和保存文档属性,但现在让我们使用来自计算器的工作脚本:Is it possible to use IronPython to return the path of the current *.dxp project as a string?输出Spotfire'打印'到文本文件

两个变量:路径和分析是“打印”的。我如何将它们打印到文本文件?

类似的东西在这里完成:http://spotfired.blogspot.co.uk/2014/04/export-image-from-visualization.html其中一个.bmp图像被创建。

非常感谢,

回答

5

写入文件不是唯一的IronPython; Python docs经过这个相当不错。在任何情况下:

f = open('c:\\filename.txt', 'w') 

f.writeline(path) 
f.writeline(analysis) 

my_list = ['one','two','three'] 

for item in my_list: 
    f.writeline(item) 

f.close 

会对你很好。

IronPython是常规Python的衍生产品,可以与.NET API(如Spotfire的API)进行交互。因此,您通常可以通过查看Python资源来找到非Spotfire特定的解决方案。

+2

嗨尼科,谢谢你的建议! open()函数会给出错误:System.ArgumentException:路径中的非法字符。当我删除c:\它运行良好(更改writeline写入后)。但是现在我无法在任何地方找到文件''filename.txt''。任何想法,:) :) – Freddy

+1

嗨弗雷迪,这是我得到写未经测试的代码;)我忽略逃避文件路径\任何\需要转义为\\。我修改了我的答案。获取需要在Python中转义的字符列表(它可以在不同语言之间变化!)检查https://docs.python.org/2.0/ref/strings.html – niko

+1

谢谢niko!我接受了你的答案,因为它给了我所要求的所有信息。现在遇到了问题“System.IO.IOException:访问路径'c:\ testSpotfireOutput.txt'被拒绝。 - > System.UnauthorizedAccessException:访问路径'c:\ testSpotfireOutput.txt'被拒绝。 “现在我必须弄清楚:) – Freddy

0

希望这会有所帮助。我使用了以下方法,因为我想将新行添加到文件中。如果您认为合适,您可以使用for循环或其他逻辑。

#importing Streamwriter from the library 
from System.IO import Path, StreamWriter 

#assigning the path 
filepath = "C:/Users/file.txt" 

#append function 'a' being used 
filevariable = StreamWriter(filepath,"a") 

# assigning first line to append to the file 
linetowrite = "ABC; 123; LMN;" 
filevariable.Write(linetowrite) 

# assigning second line to append to the file 
linetowrite2 = "XYZ; 890; PQR;" 
filevariable.Write(linetowrite2) 

# close file once you complete writing the file 
filevariable.Close()