Select to view content in your preferred language

***Field Calculator Help*** How to pad a string with periods to fit remaining field length

168
13
Jump to solution
yesterday
JM32
by
Frequent Contributor

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!

Index example.png

table example.png

  

0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
MVP Regular Contributor

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}"

View solution in original post

13 Replies
ClayDonaldsonSWCA
Frequent Contributor

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..

 

JM32
by
Frequent Contributor

@ClayDonaldsonSWCA Below is what I'm running into with the f-strings. Likely I'm entering it incorrectly, any idea whats going on?

index error.png

0 Kudos
AustinAverill
Frequent Contributor

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}' 

TonyAlmeida
MVP Regular Contributor

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}"

 

 

 

 

JM32
by
Frequent Contributor

@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.

 

index error 2.png

0 Kudos
TonyAlmeida
MVP Regular Contributor

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}"
JM32
by
Frequent Contributor

@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!

table 2.png

table 3.png

field calculator 3.png

   

0 Kudos
TonyAlmeida
MVP Regular Contributor

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!)

 

JM32
by
Frequent Contributor

@TonyAlmeida  Unless I entered it incorrectly, I keep having errors pop up when trying to run it. 

 

 

index error 3.png

0 Kudos