473,698 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pulling numbers from ASCII filename not working

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.w orkspace)
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)))

Jan 24 '06
12 1976
On Thu, 26 Jan 2006 06:39:20 GMT in comp.lang.pytho n, Dennis Lee
Bieber <wl*****@ix.net com.com> wrote:
On 25 Jan 2006 12:42:20 -0800, "IamIan" <ia****@gmail.c om> declaimed the
following in comp.lang.pytho n:

[...]
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#5 9>", line 1, in -toplevel-
a = int("099",0)
ValueError: invalid literal for int(): 099


Regards,
-=Dave

--
Change is inevitable, progress is not.
Jan 26 '06 #11
On 25 Jan 2006 12:42:20 -0800, "IamIan" <ia****@gmail.c om> 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("esri Geoprocessing.G pDispatch.1")
print gp.usage("Hills hade_sa")
print gp.usage("Raste rToOtherFormat_ conversion")
print gp.usage("Defin eProjection_man agement")

# Check license availability
gp.AddMessag e ("ArcInfo license is " + str(gp.CheckPro duct("ArcInfo") ))
gp.SetProduct( "ArcInfo")
gp.CheckOutExt ension("Spatial ")

# Set workspace
workspace = "E:\\GISTes t"
gp.workspace = workspace
gp.AddMessage( "Workspace = " + gp.workspace)

filenames = os.listdir(gp.w orkspace)
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\py thonwin\pywin\f ramework\script utils.py", line
310, in RunScript
exec codeObject in __main__.__dict __
File "E:\Documen ts and
Settings\Admin istrator\Deskto p\Ian\GIS\Pytho n\zOnly.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
Jan 26 '06 #12
Thanks for the help everyone (especially those that gave more answers
than attitude). It's working perfectly!

Ian

Feb 2 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3194
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+) that fetches the document I have code like this (in my asp page I have set codepage=65001 which is utf-8) GetObjectContext("Response").AddHeader "Content-Length", rs("DocData").ActualSize GetObjectContext("Response").contentType =...
2
3525
by: Parintas Themis STE Kardias | last post by:
I have a field with numbers and characters how can i take only the numbers Thanks
0
1420
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 depending on the code: sf = "Non-ASCII but valid filename.extension" Response.AppendHeader("Content-Disposition", "attachment; filename=""" & sf & """") proposes and saves under a UTF-8-encoded filename. ...
2
2355
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 ParseFile(string fileName) { byte data; if (File.Exists(fileName)) {
1
432
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 cell of the table values from 0 to 999999999 so for example number 1111222233334444 will look in table: // <- i'm reversing the cell's order + using this method conversion number<->ascii string will be fast
3
3028
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 unsatisfied with them. Here is what I have so far: declare @Sdate as datetime declare @Edate as datetime set @SDate = '07/01/2006' set @EDate = '12/31/2006'
12
9129
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 list all the files in it that contain a non ascii character. How would I go about doing this? Thanks,
2
3885
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 file transfer?????? -Ted
9
10027
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 may be upto 10000 digits long. These numbers are coming through a device in ascii format, I am creating a text file and saving these numbers in the file.
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.