Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm building a tool in python and for that I have a question:

I have a string such as "Denver.dwg Group Layer\Denver.dwg Annotation".

How do I assign to a variable (for later print) only the first part of the string, "Denver.dwg" ?? I need to provide a general solution, as the city name differs from case to case (it might be shorter or longer) and so counting the places until the g of dwg (10 places) won't help.

Thanks.

(I'm writing the python tool for arcgis 9.3.1)

share|improve this question
1  
This is a legitimate question, and I think many readers might be interested in the answer, but is it really an appropriate one for this site? It has nothing to do with GIS. –  whuber Dec 30 '10 at 15:44
    
I thought about it but as I am building a GIS tool I decided to go with it and ask. But if more people think like you, i'd "accept the verdict" and move it or such. –  jonatr Dec 30 '10 at 15:55
1  
It's not a GIS question, but I think it's a question that other GIS users will have. I would probably lean towards not closing it and providing links to other resources, such as existing stackoverflow.com questions or online tutorials. If it is closed, I think the question will popup again. –  Jay Cummins Dec 30 '10 at 17:12

4 Answers 4

up vote 5 down vote accepted

The code below searches for the first occurence of the ".dwg" string and assigns a new substring (containing characters up to and including that occurence).

text = "Denver.dwg Group Layer\\Denver.dwg Annotation"
ext = ".dwg"

fileNameOnly = text[:text.find(ext) + len(ext)]
print fileNameOnly

This is very basic Python string manipulation. There are loads of quick references which will help you with these most commonly used functions, for example Python Basics, Section 5: Strings.

share|improve this answer

You should be able to use this string.find method, see my example below.

s = 'Denver.dwg Group Layer/Denver.dwg Annotation"'

# this returns lowest index of first occurence of dwg
# in this case 7
idx = s.find('dwg')
print idx

# take this index and add 3 for 'dwg'
# get desired string
subs = s[:idx+3]
print subs
share|improve this answer
    
Why would someone post almost identical answer to one already posted? –  Petr Krebs Dec 30 '10 at 22:46
    
If you look at when the two answers were posted, you'll see they were posted at pretty much the same time- so the second person probably didn't see yours... –  Archaeogeek Dec 31 '10 at 13:43

I'm a python n00b too, but could you use the string split method for this? http://www.tutorialspoint.com/python/string_split.htm. If you split it on spaces, then the first value in the array would be the denver.dwg value. The following works for me in python 2.5 at the command line (not sure about any ArcGIS idiosyncracies):

str = "Denver.dwg Group Layer\Denver.dwg Annotation"

cityName = str.split(' ')

print cityName[0]

share|improve this answer
    
That works until someone sends you a 'Fort Collins.dwg' file! –  Jay Cummins Dec 31 '10 at 17:42
    
Correct- the answer below would deal with that kind of problem (or you could just rename the file so there were no spaces in the name) –  Archaeogeek Jan 3 '11 at 18:11

str = "Denver.dwg Group Layer\Denver.dwg Annotation"

you can use split

  • c = str.split("\\")

['Denver.dwg Group Layer', 'Denver.dwg Annotation']

the result is a list

d = c[0].split(".") # first term of the list ie 'Denver.dwg Group Layer'

['Denver', 'dwg Group Layer']

d[0] # first term of the list

'Denver'

  • or e = str.split(" ")

['Denver.dwg', 'Group', 'Layer\Denver.dwg', 'Annotation']

  • or f = str.split(" ")[0]

'Denver.dwg'

and

g = f.split(".")

['Denver', 'dwg']

g[0]

'Denver'

look at Dive Into Python

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.