Python Question - how do I extract a part of a string
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.
(I'm writing the python tool for arcgis 9.3.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.
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.
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.
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.