Hi Everyone,
I'm trying to create a nice road index for a map. One that looks like the screenshot below. However, I can't figure out how to create the index so that my road name listed has periods filling in the remaining space before the index cell is listed.
So far, I found some examples of python to use but nothing specific to how to enter it into the field calculator in ArcGIS Pro. The table shows my two fields, one with the street name, the other with the index cell it falls in on my map.
Any help is appreciated!
Solved! Go to Solution.
You probably have nulls, spaces and number and text.
try this,
def format_index(name, index, total_length=5): # Change to your data
name = str(name).strip() # String and strip whitespace
index = str(index).strip() # String and strip whitespace
available_space = max(1, total_length - len(name) - len(index))
dot_fill = '.' * available_space
return f"{name}{dot_fill}{index}"
this is a perfect use case for f-strings. fstring.help: Python f-string guide
f"{!ALLROADNAM!:.<10}"
examples:
f"{'Done':.<12}" # Done.........
f"{123:.>8}" # .....123
f"{'Middle':.^10}" # ..Middle..
@ClayDonaldsonSWCA Below is what I'm running into with the f-strings. Likely I'm entering it incorrectly, any idea whats going on?
The expression is evaluating your field to include double quotes, and is therefore conflicting with your syntax. Change the code in your field calc to use single quotes instead of the double quotes will resolve the error: f'{!ALLROADNAM!:.<10}'
try,
format_index(!Field1Cal!, !IndexCell!)
def format_index(name, index, total_length=5): #change total_length
name = name.strip()
index = index.strip()
dot_fill = '.' * max(1, total_length - len(name) - len(index))
return f"{name}{dot_fill}{index}"
@TonyAlmeida I tried your suggestion and am running into the issue below. Any idea what is going on? I did change the first part before the code block to match my field names.
You probably have nulls, spaces and number and text.
try this,
def format_index(name, index, total_length=5): # Change to your data
name = str(name).strip() # String and strip whitespace
index = str(index).strip() # String and strip whitespace
available_space = max(1, total_length - len(name) - len(index))
dot_fill = '.' * available_space
return f"{name}{dot_fill}{index}"
@TonyAlmeida I'm so close! It was able to run in the field calculator - but now I'd like to get dots to even out so the index values are all aligned together. Similar to the first screenshot above. Below is what I have, is there a way to align the index values?
I really appreciate your help!
try,
.ljust(total_length) # this should make sure the output string is exactly 50 characters wide
def format_index(name, index, total_length=50):
name = str(name).strip() if name else "Unknown"
index = str(index).strip() if index else "--"
available_space = max(1, total_length - len(name) - len(index))
dot_fill = '.' * available_space
return f"{name}{dot_fill}{index}".ljust(total_length)
format_index(!Field1Cal!, !IndexCell!)