Select to view content in your preferred language

IF- ELSE script in the field calculator (number classification)

1523
10
Jump to solution
05-21-2020 11:14 AM
RTMRTM
by
Emerging Contributor
I have a field where they stored phone numbers, called "Sent Phone"
in my country the phone numbers have 8 characters
I made a field called "Length" ,to find out how many characters the field has "Sent Phone"
then I made a field called "New Phone"  to classify only numbers that have 8 characters
I need to make a script that pastes the numbers that have 8 catechists in "New Phone" and if it doesn't have 8, put 0
IDSent PhoneLengthNew Phone
1310
22220
321330
4110
533445678833445678
61120
7210
812569826812569826
912560912812560912
1034235677834235677
sorry for my bad English
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

code:

func(!Sent Phone!)

pre-logic script:

# only works for a string, add some
#logic to test for str, int float etc
#then if/else it in a similar block
def func(input):
    if len(input) == 8:
        result = input
    else:
        result = '0'
    return result‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

10 Replies
JoeBorgione
MVP Emeritus

In your illustration, have you updated the Length field already, or is that to be part of the script?

My first thought is to use an update cursor with arcpy or do you want to keep this at the field calculator level?

That should just about do it....
0 Kudos
RTMRTM
by
Emerging Contributor

In your illustration, have you updated the Length field already, or is that to be part of the script?

R//I currently have the length field updated

 

My first thought is to use an update cursor with arcpy or do you want to keep this at the field calculator level?

R// I would like to keep it from the field calculator

0 Kudos
DavidPike
MVP Frequent Contributor

code:

func(!Sent Phone!)

pre-logic script:

# only works for a string, add some
#logic to test for str, int float etc
#then if/else it in a similar block
def func(input):
    if len(input) == 8:
        result = input
    else:
        result = '0'
    return result‍‍‍‍‍‍‍‍‍‍‍‍
JoshuaBixby
MVP Esteemed Contributor

In addition to David's answer, this can be accomplished with a one-liner in the expression block that doesn't require a pre-calculated length field:

!Sent Phone! if len(!Sent Phone!) == 8 else '0'
0 Kudos
DavidPike
MVP Frequent Contributor

on looking back at my answer I'm also missing quotes to make 0 a string. Have edited or will try if able. Loving the one liner Joshua!

0 Kudos
RTMRTM
by
Emerging Contributor
I have a problem:

the script works perfectly, add it as precess in modelbuilder

it works for me when I have modelbuilder open,
but when I run it from the sale of parameters it doesn't work
returns this error
0 Kudos
DavidPike
MVP Frequent Contributor

Sharing the full error message would be helpful

RTMRTM
by
Emerging Contributor

Executing (Calculate Field): CalculateField "C:\Users\Shp_salida\001.shp"
 Phone func( !Telefono_C! ) PYTHON_9.3 "def func(input):\n    if len(input) == 8:\n        result = input\n    else:\n        result = 0\n    return result\n"
Start Time: Thu May 21 15:59:34 2020
ERROR 000539: Error running expression: func( 5 )
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 2, in func
TypeError: object of type 'int' has no len()

Failed to execute (Calculate Field).
Failed at Thu May 21 15:59:34 2020 (Elapsed Time: 0.01 seconds)
Failed to execute (Model72).
Failed at Thu May 21 15:59:34 2020 (Elapsed Time: 1.34 seconds)

0 Kudos
DavidPike
MVP Frequent Contributor

Ok so an integer is being passed into the function. Replace if len(input) with if len(str(input))