Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 12th, 2013, 10:35 AM
 WV-Mike
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 36 m 5 sec
Reputation Power: 0
How do I run a script in IDLe

*First post from newbie*
```````````````````````````
I have never used Python before.
I thought this would be obvious, but to me it is not.

I was sent a script by someone.
Here are their instructions:

"With 5 lines of code you can split the string of coordinates into a list, reverse the list, and join it back together into a long string. The printed result can be copied and pasted into the KML and the elevation profile will be reversed. The code below will do the trick."

Here is the script:

# Python script to reverse KML coordinates, just paste coordinates into KMLcoords variable and run script
KMLcoords = ''' PASTE COORDINATES HERE, BETWEEN TRIPLE QUOTES '''
coordsSplit = KMLcoords.split(' ')
coordsReversed = coordsSplit[::-1]
coordsJoined = ' '.join(coordsReversed)
print coordsJoined
# end python script

Here is what I pasted into the IDLE prompt as a test:

# Python script to reverse KML coordinates, just paste coordinates into KMLcoords variable and run script
KMLcoords = '''-79.7962560949221,39.5034318624616,0
-79.7963859014283,39.5034814389697,0
-79.7970197879323,39.5037059595619,0
-79.7979009449095,39.5040388009614,0
-79.7984644303751,39.5042449905803,0
-79.7989934019142,39.5044450976328,0
-79.7997323475272,39.5047217540965,0
-79.800625474953,39.5050555724905,0
'''
coordsSplit = KMLcoords.split(' ')
coordsReversed = coordsSplit[::-1]
coordsJoined = ' '.join(coordsReversed)
print coordsJoined
# end python script

I thought I would find a "Run Script" in the menu, but I don't see one. So, after pasting in the above I hit "Enter".
The results at the prompt:
SyntaxError: multiple statements found while compiling a single statement.

Obviously, I am cluesless.
What do I need to do to run the script?
Thanks,
WV-Mike

#2
February 12th, 2013, 12:21 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
I try to avoid idle, but anyway
click File
select new window
paste the code into new window
click run
or maybe save then run

The program won't work as you expect because it's set up to use a space character as a record delimiter. You'll need to change the data all on one line as
Code:
`'-79.7962560949221,39.5034318624616,0 -79.7963859014283,39.5034814389697,0`
or change both occurrences of ' ' to '\n' .
__________________
[code]Code tags[/code] are essential for python code!

#3
February 12th, 2013, 01:08 PM
 WV-Mike
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 36 m 5 sec
Reputation Power: 0
How do I run a script in IDLe

Quote:
 Originally Posted by b49P23TIvg I try to avoid idle, but anyway click File select new window paste the code into new window click run or maybe save then run The program won't work as you expect because it's set up to use a space character as a record delimiter. You'll need to change the data all on one line as Code: `'-79.7962560949221,39.5034318624616,0 -79.7963859014283,39.5034814389697,0` or change both occurrences of ' ' to '\n' .

Greetings,
I think I followed your instructions correctly.
But, I get and error.
Here is a screen shot: http://epicroadtrips.us/py/
Thanks,
WV-Mike

#4
February 12th, 2013, 01:24 PM
 dariyoosh
Contributing User

Join Date: Nov 2012
Location: Iran
Posts: 148
Time spent in forums: 2 Days 15 h 7 m 17 sec
Reputation Power: 139
@WV-Mike
Why not simply put the code in a file edited by a good text editor (for example notepad++ or any editor of your favorite) and run the script directly from a terminal? Is there any particular reason that you have to use IDLE?

Regards,
Dariyoosh

#5
February 12th, 2013, 01:26 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
Yes, beautiful scenery. If you're using python 3 you'd need
print(coordsJoined)
because print is a function in python 3 whereas it was a statement in earlier python versions.

#6
February 12th, 2013, 05:02 PM
 WV-Mike
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 36 m 5 sec
Reputation Power: 0
Quote:
 Originally Posted by dariyoosh @WV-Mike Why not simply put the code in a file edited by a good text editor (for example notepad++ or any editor of your favorite) and run the script directly from a terminal? Is there any particular reason that you have to use IDLE? Regards,Dariyoosh

Yes.
I am more used to a GUI.
And, I would not know how to run the script from the command line either.

I tried to accomplish this in Excel but it exceeds what it will work with.

Thanks,
WV-Mike

#7
February 12th, 2013, 05:21 PM
 WV-Mike
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 36 m 5 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg Yes, beautiful scenery. If you're using python 3 you'd need print(coordsJoined) because print is a function in python 3 whereas it was a statement in earlier python versions.

Using print(coordsJoined) did the trick.
Script ran fine and output was what I wanted.
Thanks!
-Mike

#8
February 13th, 2013, 01:38 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 571
Time spent in forums: 5 Days 2 h 37 m 24 sec
Reputation Power: 63
Are we getting into Python best editor wars?

If you are a beginner with Python coding, IDLE can be very helpful once you understand that the code written in the editor has to be saved (Ctrl s or File/Save) before it can be run (F5 or Run/Run Module). You have to configure IDLE (Options) to come up in the editor and not the interactive shell ('>>>' prompts).
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How do I run a script in IDLe