Pulling numbers from ASCII filename not working

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jan 25 18:38:38 EST 2006


On Wed, 25 Jan 2006 12:42:20 -0800, IamIan wrote:

> Thank you for the replies, I'm new to Python and appreciate your
> patience. I'm using Python 2.1.
> 
> To reiterate, the ASCII files in the workspace are being read correctly
> and their latitude values (coming from the filenames) are successfully
> being converted to string. Even doing LatInt = int(LatString) works,
> however the second I try to print LatInt's value or use it in
> mathematical operations, the code chokes in ArcGIS.

[snip]

>    LatString = str(Latitude)
>    LatInt = int(LatString)
>    gp.AddMessage("LatInt is " + LatInt)

Dude. You're trying to add a string to an int. What did you think would
happen?

> The complete traceback:
> 
> Traceback (most recent call last):
>   File "e:\python21\pythonwin\pywin\framework\scriptutils.py", line
> 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "E:\Documents and
> Settings\Administrator\Desktop\Ian\GIS\Python\zOnly.py", line 32, in ?
>     gp.AddMessage("LatInt is " + LatInt)
> TypeError: cannot add type "int" to string

The traceback tells you exactly what is wrong, and where it is going
wrong: you are trying to add a string to an int.

What you probably want is either gp.AddMessage("LatInt is " + LatString)
or gp.AddMessage("LatInt is %d" % LatInt).


-- 
Steven.




More information about the Python-list mailing list