1

I have been struggling with something I hope to be a simple question. I would like to output values (String / Real / Stringlist) from Spotfire to a text file on my computer. My final solution would need document properties to be listed and saved, but for now let's use a working script from stackoverflow: Is it possible to use IronPython to return the path of the current *.dxp project as a string?

Two variables: path and analysis are 'printed'. How can I print those to a text file?

Something similar is done here: http://spotfired.blogspot.co.uk/2014/04/export-image-from-visualization.html where a .bmp image is created.

Many thanks,

Community
  • 1
  • 1
Freddy
  • 419
  • 8
  • 16

2 Answers2

5

writing to a file isn't unique to IronPython; the Python docs go over this fairly well. in any case:

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()

will do you fine.

IronPython is a derivative of regular Python that can interact with .NET APIs such as Spotfire's API. thus, you can typically find non-Spotfire-specific solutions by looking at Python resources.

M463
  • 2,003
  • 3
  • 23
  • 39
niko
  • 3,946
  • 12
  • 26
  • 2
    Hi Niko, thanks for your advice! the open() function gives the error: System.ArgumentException: Illegal characters in path. When i remove c:\ it runs fine (after changing writeline to write). However now I cannot find the file ''filename.txt'' anywhere. Any ideas on that :) – Freddy Aug 27 '15 at 09:21
  • 1
    hi Freddy, this is what I get for writing untested code ;) I neglected to escape the \ in the file path; any \ needs to be escaped as \\. I've modified my answer. for a list of characters which need to be escaped in Python (it can vary between languages!) check https://docs.python.org/2.0/ref/strings.html – niko Aug 27 '15 at 09:25
  • 1
    Thanks niko! I accepted your answer, as it gives me all information I asked for. Now ran into the issue "System.IO.IOException: Access to the path 'c:\testSpotfireOutput.txt' is denied. ---> System.UnauthorizedAccessException: Access to the path 'c:\testSpotfireOutput.txt' is denied." Now I have to figure that out :) – Freddy Aug 27 '15 at 09:29
  • 1
    if you are using a work PC you may not have permissions to write to C: directly. you can try a path like %TEMP% (should default to `C:\Users\[your user name]\AppData\Local\Temp` on Windows 7) or another known path like a folder on your desktop. – niko Aug 27 '15 at 09:33
  • 2
    Answered the question nicely but the extra info about more resources really earned my up vote. You find so many neat solutions to common issues by knowing this. – RyanfaeScotland Sep 03 '15 at 16:20
0

Hope this helps. I used the following approach since I wanted to append new lines to a file. You can use for loops or other logic as you see fit.

#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()
Alex Jacob
  • 79
  • 2
  • 5