Select to view content in your preferred language

How to update a double field from a text field

771
7
Jump to solution
08-15-2023 01:56 PM
Laura
by MVP Regular Contributor
MVP Regular Contributor

I keep getting errors that it does not append when trying to do this. 

1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Yes. If it can't convert the string into a number, the conversion will fail. You need to strip out the character:

# NewField = 
convert(!OldField!)

# Code Block
def convert(x):
    try:
        x = x.replace('"', '')
        return float(x)
    except:
        return None

Have a great day!
Johannes

View solution in original post

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

You can't update the field type if there is data in the field.

Perhaps you can elaborate on what you are trying to do for alternate suggestions


... sort of retired...
0 Kudos
Laura
by MVP Regular Contributor
MVP Regular Contributor

i know that. the only work around was to create another field with the desired data type and fill it, however that is not working

0 Kudos
DanPatterson
MVP Esteemed Contributor

 

Or if you don't want to use a code block... (substitute !junk! for your field name of course)

float(!junk!) if !junk!.isnumeric() else None

 

float2.png

float.png


... sort of retired...
JohannesLindner
MVP Frequent Contributor

Create a new Double field, then run Calculate Field with this Python expression (note the exclamation marks in line 2):

# NewField = 
convert(!OldField!)

# Code Block
def convert(x):
    try:
        return float(x)
    except:
        return None

JohannesLindner_0-1692135413257.png


Have a great day!
Johannes
Laura
by MVP Regular Contributor
MVP Regular Contributor

These sound great. My specific issue is related to pipe diameter. So they have the " inch sign attached to the numbers, and that may be why it is not going into the double field?

0 Kudos
JohannesLindner
MVP Frequent Contributor

Yes. If it can't convert the string into a number, the conversion will fail. You need to strip out the character:

# NewField = 
convert(!OldField!)

# Code Block
def convert(x):
    try:
        x = x.replace('"', '')
        return float(x)
    except:
        return None

Have a great day!
Johannes
0 Kudos
Laura
by MVP Regular Contributor
MVP Regular Contributor

Awesome, that seemed to do the trick, thanks!

0 Kudos