I searched the archives but couldn't find anyone else with this
problem. Basically I'm grabbing all ASCII files in a directory and
doing geoprocessing on them. I need to calculate a z-factor based on
the latitude of the ASCII file being worked on, which is in the
filename. If I type in the code manually it works and reads the
latitude value from the ASCII filename, but when run within ArcGIS it
crashes when it gets to int(LatString). Isnumber() returned false for
Latitude as well. Is there something different about reading values
from an ASCII filename?
import sys, os, win32com.client, string, gc
# Get a list of ASCII files in the workspace for ASCII To Raster
conversion
filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
for filename in filenames
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
for filename in filenames:
# For each ASCII file, create Hillshade.
# account for latitude by computing Z units using radians
Latitude = filename[1:3]
LatString = str(Latitude)
LatInt = int(LatString)
radians = LatInt * 0.0174532925
zFactor = 1/(113200 * (cos(radians))) 12 1951
First: http://www.catb.org/~esr/faqs/smart-questions.html
You say your program 'crashes' when it gets to 'int(LatString)'. I'm
guessing (and it's entirely a guess, since you don't tell us) that you
are getting an exception like 'ValueError: invalid literal for int():
whatever'. I'm guessing again that 'LatString' doesn't contain what
you think it does at this point. Have you tried inserting a print
statement to show you just what 'LatString' contains?
At lest one of the filenames (or directories) being returned by
os.listdir doesn't have integer value where you are looking.
Remember that os.listdir returns subdirectories as well as files.
You may want to look at using glob.glob() instead and limit it
to *.asc.
Secondly,
filenames = [filename.lower() \
for filename in filenames \
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
is better rewritten as (not tested):
import glob
filenames=glob.glob(os.path.join(gp.workspace, '*.asc'))
filenames = [f.lower() for f in filenames if not f.startswith('-')]
Larry Bates
IamIan wrote: I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based on the latitude of the ASCII file being worked on, which is in the filename. If I type in the code manually it works and reads the latitude value from the ASCII filename, but when run within ArcGIS it crashes when it gets to int(LatString). Isnumber() returned false for Latitude as well. Is there something different about reading values from an ASCII filename?
import sys, os, win32com.client, string, gc
# Get a list of ASCII files in the workspace for ASCII To Raster conversion filenames = os.listdir(gp.workspace) filenames = [filename.lower() for filename in filenames if (filename[-4:].lower() == ".asc" and filename[0] != "-" )] for filename in filenames:
# For each ASCII file, create Hillshade. # account for latitude by computing Z units using radians Latitude = filename[1:3] LatString = str(Latitude) LatInt = int(LatString) radians = LatInt * 0.0174532925 zFactor = 1/(113200 * (cos(radians)))
The exception I get is "TypeError: Cannot add value 'int' to string." I
have looked at LatString, and it is the string representation of
latitude ('17' etc.). What's odd is that the exception is raised not
when I include LatInt = int(LatString), but when I try to print
LatInt's value or multiply it by another number.
Filenames are along the lines of "N16W110.asc" - is there another way
to get LatString into a number for multiplication purposes?
"IamIan" <ia****@gmail.com> wrote: The exception I get is "TypeError: Cannot add value 'int' to string."
now that you've posted the exception, can you please post the code
you're using, and the *complete* traceback.
(the stuff you posted earlier contained syntax errors, and didn't con-
tain any additions. I suggest using copy/paste, rather than typing
things from memory).
</F>
On 24 Jan 2006 10:44:32 -0800, "IamIan" <ia****@gmail.com> wrote: I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based on the latitude of the ASCII file being worked on, which is in the filename. If I type in the code manually it works and reads the latitude value from the ASCII filename, but when run within ArcGIS it crashes when it gets to int(LatString). Isnumber() returned false for Latitude as well. Is there something different about reading values from an ASCII filename?
Aren't you curious as to what the value of LatString was that failed?
Don't you know how to find out? import sys, os, win32com.client, string, gc
# Get a list of ASCII files in the workspace for ASCII To Raster conversion filenames = os.listdir(gp.workspace) filenames = [filename.lower() for filename in filenames if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
indentation of the above two lines would improve readability
for filename in filenames:
I would try print repr(filename) here, to see what you are dealing with # For each ASCII file, create Hillshade. # account for latitude by computing Z units using radians Latitude = filename[1:3] LatString = str(Latitude)
you probably won't need a print repr(LatString) here if you see the above print LatInt = int(LatString) radians = LatInt * 0.0174532925 zFactor = 1/(113200 * (cos(radians)))
BTW, capitalizing the first letter of python variable names is counter to usual convention.
Regards,
Bengt Richter
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.
My full code:
# Import system modules
import sys, os, win32com.client
# Create the geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDisp atch.1")
print gp.usage("Hillshade_sa")
print gp.usage("RasterToOtherFormat_conversion")
print gp.usage("DefineProjection_management")
# Check license availability
gp.AddMessage ("ArcInfo license is " + str(gp.CheckProduct("ArcInfo")))
gp.SetProduct("ArcInfo")
gp.CheckOutExtension("Spatial")
# Set workspace
workspace = "E:\\GISTest"
gp.workspace = workspace
gp.AddMessage("Workspace = " + gp.workspace)
filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
for filename in filenames
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
for filename in filenames:
# For each ASCII file, create Hillshade.
# account for latitude by computing Z units using radians
Latitude = filename[1:3]
LatString = str(Latitude)
LatInt = int(LatString)
gp.AddMessage("LatInt is " + LatInt)
radians = LatInt * 0.0174532925
zFactor = 1/(113200 * (cos(radians)))
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\zOnl y.py", line 32, in ?
gp.AddMessage("LatInt is " + LatInt)
TypeError: cannot add type "int" to string
I tried print repr(filename) and it returned the actual filename:
'n16w099.asc' , 'n17w062.asc' , etc.
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\zOnl y.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.
>Dude. You're trying to add a string to an int. What did you think would happen?
Dude. I thought it would concatenate the value for LatInt with the rest
of the sentence; I wasn't literally trying to add them. Apparently you
can only concatenate strings like this in Python.
On Wed, 25 Jan 2006 16:57:47 -0800, IamIan wrote: Dude. You're trying to add a string to an int. What did you think would happen?
Dude. I thought it would concatenate the value for LatInt with the rest of the sentence; I wasn't literally trying to add them. Apparently you can only concatenate strings like this in Python.
No. An interactive session is your friend. You can concatenate all sorts
of things, not just strings: [2, 3, 4] + [5]
[2, 3, 4, 5] (2, 3, 4,) + (5,)
(2, 3, 4, 5)
What should 4 + "5" give you? 9 or "45"?
Your description of the problem was *utterly* wrong. You said you get an
error when you try to print LatInt; but that's not true, is it? print
LatInt works fine, I bet. If you had actually posted the traceback, rather
than trying to describe it in your own words, your problem would have been
solved after one post. In other words, you sent us all on a wild goose
chase.
But the main thing is, the traceback was telling you exactly what was
wrong. Listen to it, it knows.
--
Steven.
On Thu, 26 Jan 2006 06:39:20 GMT in comp.lang.python, Dennis Lee
Bieber <wl*****@ix.netcom.com> wrote: On 25 Jan 2006 12:42:20 -0800, "IamIan" <ia****@gmail.com> declaimed the following in comp.lang.python:
[...] I tried print repr(filename) and it returned the actual filename: 'n16w099.asc' , 'n17w062.asc' , etc.
You may have problems with the longitude... those leading zeroes may be taken as Octal notation...
Shouldn't be a problem unless you make it one. Int defaults to
decimal, unless you specify a base or tell it to infer the base from
the number format by specifying a base of zero. a = int("062") a
62 a = int("062",0) a
50
Hard to interpret "099" as an octal number in any case:
a = int("099",0)
Traceback (most recent call last):
File "<pyshell#59>", line 1, in -toplevel-
a = int("099",0)
ValueError: invalid literal for int(): 099
Regards,
-=Dave
--
Change is inevitable, progress is not.
On 25 Jan 2006 12:42:20 -0800, "IamIan" <ia****@gmail.com> 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.
My full code:
# Import system modules import sys, os, win32com.client
# Create the geoprocessor object gp = win32com.client.Dispatch("esriGeoprocessing.GpDisp atch.1") print gp.usage("Hillshade_sa") print gp.usage("RasterToOtherFormat_conversion") print gp.usage("DefineProjection_management")
# Check license availability gp.AddMessage ("ArcInfo license is " + str(gp.CheckProduct("ArcInfo"))) gp.SetProduct("ArcInfo") gp.CheckOutExtension("Spatial")
# Set workspace workspace = "E:\\GISTest" gp.workspace = workspace gp.AddMessage("Workspace = " + gp.workspace)
filenames = os.listdir(gp.workspace) filenames = [filename.lower() for filename in filenames if (filename[-4:].lower() == ".asc" and filename[0] != "-" )] for filename in filenames:
# For each ASCII file, create Hillshade. # account for latitude by computing Z units using radians Latitude = filename[1:3] LatString = str(Latitude) LatInt = int(LatString)
^^^^^^--here you set LatInt to an integer
gp.AddMessage("LatInt is " + LatInt) radians = LatInt * 0.0174532925 zFactor = 1/(113200 * (cos(radians)))
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\zOn ly.py", line 32, in ? gp.AddMessage("LatInt is " + LatInt)
string--^^^^^^^^^^^^ ^^^^^^--integer
(LatString in place of LatInt might work, since it's a string (so is Latitude))TypeError: cannot add type "int" to string
^^^^^^^^^^ ^^^ ^^^^^^
This is not lying ;-)
I tried print repr(filename) and it returned the actual filename: 'n16w099.asc' , 'n17w062.asc' , etc.
So you can see Latitude would be '16' '17' etc. right?
On to the next traceback ;-)
Regards,
Bengt Richter
Thanks for the help everyone (especially those that gave more answers
than attitude). It's working perfectly!
Ian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hans |
last post by:
Hi!
I have a classic ASP site (not ASP.NET) where the user is able to download
documents. The documents are stored inside an Oracle database set up for
UTF-8 encoding. In the VB6 code (COM+)...
|
by: Parintas Themis STE Kardias |
last post by:
I have a field with numbers and characters how can i take only the numbers
Thanks
|
by: Costas Andriotis |
last post by:
This is rather a IE-specific problem, but maybe someone has a solution. The HTTP headers are correct (Gecko for example understands and downloads/saves the file correctly) but IE does the wrong thing...
|
by: dotnetchic |
last post by:
I'm trying to parse a binary file to help me with some analysis...what
I'd like to do is take a byte of data and display its ascii char value
(37='%', 38='&', etc.).
private void...
|
by: peter_k |
last post by:
Hi,
I've to implement fast library for big numbers arithmetics. I'll store
the number in the table of unsigned long variables. And now i've a
question: what will be faster:
a) storing in one...
|
by: rugger81 |
last post by:
I am currently working in the sql server 2000 environment and I want to
write a function to pull all dates within a given date range. I have
created several diferent ways to do this but I am...
|
by: bg_ie |
last post by:
Hi,
I'm updating my program to Python 2.5, but I keep running into
encoding problems. I have no ecodings defined at the start of any of
my scripts. What I'd like to do is scan a directory and...
|
by: tedpottel |
last post by:
Hi,
My program has the following code to transfer a binary file
f = open(pathanme+filename,'rb')
print "start transfer"
self.fthHandle.storbinary('STOR '+filename, f)
How can I do an ASCII...
|
by: Aamir Mahmood |
last post by:
Hi,
I have working on a system in which I have to manipulate *very* big numbers.
Like
32368060745625089670148189374568111100874165870871388541651800834565616109380834613212956588769877
They...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |