Select to view content in your preferred language

Help with Python Code/Field Calculator

544
7
Jump to solution
03-28-2024 07:35 AM
BETAGIS
New Contributor II

Hi. I have a String Type field in a large attribute table. I want to add a "-0" a the end of each entry or "-1", "-2", etc. See examples below:

Example entries in this case would be:

CROOKED ST 1

CROOKED ST 2

CROOKED ST 3

CROOKED ST 4

CROOKED ST 5

CROOKED ST 6

CROOKED ST 7

CROOKED ST 8

CROOKED ST 9

CROOKED ST 10

 

How can I add the '-0' to the end of each entry so it'd look like:

 

CROOKED ST-01

CROOKED ST-02

CROOKED ST-03

CROOKED ST-04

CROOKED ST-05

CROOKED ST-06

CROOKED ST-07

CROOKED ST-08

CROOKED ST-09

CROOKED ST-10

 

Anyone know if this is possible? Even if I cant change the double digit values proceeding after the 10th entry it would be a great help.

 

Thanks!

0 Kudos
1 Solution

Accepted Solutions
AllenDailey1
Occasional Contributor III

Hello,

Here is a function / code block I wrote that does what you're describing:

def number_streets(street):
    components = street.split(" ")
    num = components[-1]
    if len(num) == 1:
        num = "-0" + num
    else:
        num = "-" + num
    stname = " ".join(components[:-1])
    result = stname + num
    return result

If you want to test it out, here's an example of how I tested it:

sts = ["MY ST 1", "MY ST 2", "MY ST 10", "MY ST 41"]
for st in sts:
    print(number_streets(st))

The results I got were:

MY ST-01
MY ST-02
MY ST-10
MY ST-41

For a field calculation, the expression would be:

number_streets(!YOUR_FIELD_NAME!)

I'd be interested to hear if you use this and/or if it works for your purposes.

View solution in original post

7 Replies
MErikReedAugusta
Occasional Contributor III

Depending on a few things, you could do this with a regex calculation, or you can brute-force it.

  1. Are the numbers already present?
  2. Are the numbers all formatted per your first example?
    1. Numbers are not zero-padded
    2. Always a space before the number
  3. How high do the numbers go?
  4. How many roadway types do you have in this dataset (e.g., ST, DR, RD, STREET)
    1. Be exacting here: "ST" and "STREET" would likely need to be treated differently.
0 Kudos
BETAGIS
New Contributor II

Hi, thank you for the response. I have an answer for each of your questions:

1.) Yes the numbers are already present

2.) Yes, all numbers are per my first example throughout the dataset - always a space before the number.

3.) The highest number is 115 on one of the larger length roads in the dataset.

4.) There are 10 roadway types in total - they are all abbreviated: ST, PL, DR, RD, PKWY, PK, AVE, WAY, TER, and CT

 

If you think it's better to do it "manually" - I'll trust your judgement. Thanks again!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If the max number goes to 115, do you want the result to be "001" so that all the values are 3 characters. 

0 Kudos
BETAGIS
New Contributor II

No, I'd prefer the numbers to be "01" and then be 3 characters if necessary.

 

0 Kudos
AllenDailey1
Occasional Contributor III

Hello,

Here is a function / code block I wrote that does what you're describing:

def number_streets(street):
    components = street.split(" ")
    num = components[-1]
    if len(num) == 1:
        num = "-0" + num
    else:
        num = "-" + num
    stname = " ".join(components[:-1])
    result = stname + num
    return result

If you want to test it out, here's an example of how I tested it:

sts = ["MY ST 1", "MY ST 2", "MY ST 10", "MY ST 41"]
for st in sts:
    print(number_streets(st))

The results I got were:

MY ST-01
MY ST-02
MY ST-10
MY ST-41

For a field calculation, the expression would be:

number_streets(!YOUR_FIELD_NAME!)

I'd be interested to hear if you use this and/or if it works for your purposes.

BETAGIS
New Contributor II

Thanks a bunch! That did exactly what I was trying to do. Will keep this in the back pocket for future uses.

0 Kudos
AllenDailey1
Occasional Contributor III

Awesome, I'm glad!

0 Kudos