【问题标题】:Output Spotfire 'print' to text file将 Spotfire 'print' 输出到文本文件
【发布时间】:2023-10-16 18:06:01
【问题描述】:

我一直在努力解决我希望是一个简单的问题。我想将 Spotfire 中的值(字符串/实数/字符串列表)输出到我计算机上的文本文件中。我的最终解决方案需要列出和保存文档属性,但现在让我们使用来自 * 的工作脚本:Is it possible to use IronPython to return the path of the current *.dxp project as a string?

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

这里做了类似的事情:http://spotfired.blogspot.co.uk/2014/04/export-image-from-visualization.html 创建 .bmp 图像。

非常感谢,

【问题讨论】:

    标签: ironpython spotfire


    【解决方案1】:

    写入文件并不是 IronPython 独有的; Python docs 很好地解决了这个问题。无论如何:

    f = open('c:\\filename.txt', 'w')
    
    f.write(path)
    f.write(analysis)
    
    my_list = ['one','two','three']
    
    for item in my_list:
        f.write(item)    # write the item's content
        f.write('\n')    # add a line break (optional)
    
    f.close()
    

    你会没事的。

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

    【讨论】:

    • 您好 Niko,感谢您的建议! open() 函数给出错误: System.ArgumentException: Illegal characters in path。当我删除 c:\ 它运行良好(在将 writeline 更改为 write 之后)。但是现在我在任何地方都找不到文件“filename.txt”。任何想法:)
    • 嗨弗雷迪,这是我编写未经测试的代码所得到的;)我忽略了在文件路径中转义\;任何 \ 都需要转义为 \\。我修改了我的答案。对于需要在 Python 中转义的字符列表(它可能因语言而异!)检查docs.python.org/2.0/ref/strings.html
    • 谢谢尼科!我接受了你的回答,因为它提供了我要求的所有信息。现在遇到了问题“System.IO.IOException:对路径'c:\testSpotfireOutput.txt'的访问被拒绝。---> System.UnauthorizedAccessException:对路径'c:\testSpotfireOutput.txt'的访问被拒绝。 "现在我必须弄清楚:)
    • 如果您使用的是工作 PC,您可能没有权限直接写入 C:。您可以尝试使用 %TEMP% 之类的路径(在 Windows 7 上应默认为 C:\Users\[your user name]\AppData\Local\Temp)或其他已知路径(例如桌面上的文件夹)。
    • 很好地回答了这个问题,但是关于更多资源的额外信息确实赢得了我的支持。通过了解这一点,您可以找到许多解决常见问题的巧妙方法。
    【解决方案2】:

    希望这会有所帮助。我使用了以下方法,因为我想将新行附加到文件中。您可以根据需要使用 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()
    

    【讨论】: