file input

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
kalassak
Posts: 2
Joined: Tue May 24, 2016 9:52 am

file input

Post by kalassak »

Hello,

I'm working on writing a script which would read a text file on the internet (or on my disk) and parse it for a latitude-longitude pair, and maybe some file name information. I've done some testing and I've been unable to import and use python's urllib2 or the built-in file operation open().

The code I have so far looks like this:

Code: Select all

input=open("coords.txt",'r')
m=input.readlines()
input.close()
l = m[0].strip().split(',')

#... loading the image data and adjusting panel properties

panel[0].setCenter(float(l[0]),float(l[1]),scale=5)
#... saving the file


It seems the input=open() line is the only line causing problems. The script works just fine when floats are hard-coded into the setCenter() function. According to this documentation I found, what I'm doing should work, but the script doesn't save the file. So, I'm not sure to what extent the code works or if it just errors out. The script is running in cmd on Windows 7 from python as a subprocess.

How do I properly read data from a text file into a script?

Thanks,

Kalassak
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: file input

Post by joleenf »

Hi,

Are you trying to save the image or save another text file?

I tested a variation of your post in my jython shell and had no problem reading a file as you had posted. Do you intend to display only one coordinate pair from the text file?

I have worked with the script you posted and added file writing and image capture. A few things to note about the attached code:

1.) expandpath should capture the user's home directory.

2.) os.path.join should ensure that the directory slashes are correct for your machine

3.) The file write() method expects a single string. See Learn Python The Hard Way for one explanation

4.) When writing scripts, I find it easier to test in the jython shell before I run from the command line. This helps me remove python errors and verify that the procedure is correct before proceeding to a command line run.

Screen Shot 2016-05-28 at 7.34.42 AM.png


Here is the code that I tested in my jython shell:

Code: Select all

panel=buildWindow()

userPath=expandPath('~')

inputFile = os.path.join(userPath, "coords.txt")
outTextFile = os.path.join(userPath,"coordsnew.txt")

input=open(inputFile,'r')
m=input.readlines()
input.close()

# save new text file
output=open(outTextFile, 'w')


for index,coordPair in enumerate(m):
    [lat, lon] = coordPair.strip().split(',')
    panel[0].setCenter(float(lat), float(lon), scale=5)  # scaling each time may be too much

    # save an image after each setCenter
    counter = str(index).zfill(3)
    outFile = '{}someImage.gif'.format(counter)
    outputFile = os.path.join(userPath, outFile)
    panel[0].captureImage(outputFile)

    #write to text file
    output.write(lat + lon + "\n" )

# close output file
output.close()
print 'Done'



I hope this is what you needed. If not, please post again.

Joleen
User avatar
kalassak
Posts: 2
Joined: Tue May 24, 2016 9:52 am

Re: file input

Post by kalassak »

Hello,

Yes, that helped. I realized I was assuming that the script was running in its original directory and not McIDAS-V's directory. Now that I realized that, it works now. Thank you for your help.

- Kalassak
Post Reply