Select to view content in your preferred language

# How to update a double field from a text field

809
7
08-15-2023 01:56 PM
by
MVP Regular Contributor

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

1 Solution

Accepted Solutions
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
7 Replies
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...
by
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

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

... sort of retired...
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``````

Have a great day!
Johannes
by
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?

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
by
MVP Regular Contributor

Awesome, that seemed to do the trick, thanks!