Hi, I am a beginner-novice programmer using Python to build custom tools in ESRI's ArcGIS. I have what seems like a simple problem, but I am afraid I'm missing a core concept. Here goes...
In this simple example I assign string values to some variables and then change the text of a map layout text element using a hard-coded format.
COUNTY = "Tioga" TOWN = "Wellsboro" someText.text = COUNTY + " - " + TOWN # someText would now read: Tioga - Wellsboro
Now I would like the user to pass a custom format as a parameter.
locTextFormat = arcpy.GetParameterAsText(4) # what the user passed to locTextFormat: COUNTY + " --- " + TOWN COUNTY = "Tioga" TOWN = "Wellsboro" someText.text = locTextFormat # now, as expected, someText reads: COUNTY + " --- " + TOWN # but, I would like someText to read: Tioga --- Wellsboro
How do I get python to see the string passed into locTextFormat as two variables concatenated with a string?
I have been digging hard on this one for a couple of days now. I have seen a similar question that was solved using a dictionary (see it here) but although this allows a user to submit different choices, they are still limited by four hard-coded choices. I am looking for my user to enter an unlimited number of valid format strings and get a custom result. It is important to note that the variables COUNTY and TOWN will be assigned values dynamically by looping through several features in a map extent; it is not an option for the user to provide the town/county names at run-time because they will change. An abbreviated version of my final code would look something like:
# I am dynamically getting the town and county name for several features in my map extent and then building a stacked text block to assign to someText locTextFormat = arcpy.GetParameterAsText(4) # what the user passed to locTextFormat: COUNTY + " --- " + TOWN towns = arcpy.SearchCursor(locLyr) x = 0 for town in towns: TOWN = town.TOWN COUNTY = town.COUNTY if x == 0: locale = locTextFormat x += 1 else: locale += '\r' + locTextFormat x += 1 someText.text = locale # again, I realize this code as written is simply building locale with literal strings of the user input; I am looking to have locTextFormat be read as a concatenation of variables and strings
In the code above, if I had three towns/counties in my map extent, I would like to see someText read something like:
Tioga --- Wellsboro
Washington --- Waynesburg
Erie --- Buffalo
Sorry if this was a bit long but I want everyone to know exactly what I am trying to do here. Thanks in advance for any insight you may be able to provide.