[GIS] Convert a string field to a number field using arcpy

arcgis-desktoparcpypython

Accidentally posted this in the general stack overflow forum first.

I have a large (>1000) number of shape-files in which there are fields containing numbers that are defined as text fields. I need to have a fields containing these values as numbers. I can add the new fields, but I'm failing to when I try to populate them.

I'm using ArcGis 10.1. Fields may have values ranging from 0-10, and including up to one decimal place, or they may be empty for a variable (actually blank, no placeholder).

Below is the python script I'm using for two of the variables (N_CT and N_CFY), and the error I get. I think my problem is in how to transfer the text value into the Decimal conversion.

I'm new to scripting, so please excuse me if my description or word choices are unclear.

import arcpy, os, sys
from arcpy import env
from decimal import *

arcpy.env.overwriteOutput = True

env.workspace = "C:\Users\OuelletteMS\Desktop\Ice_Data_testarea"

listFCs = arcpy.ListFeatureClasses("*")  
for fc in listFCs:
    print str("processing " + fc) # displays the file that is currently being handled

    strNCT = "N_CT"   # the current, text version of the field             
    newNCT = "NCT"    # the new, number version I want to create         
    strNCFY = "N_CFY" # the current, text version of the field           
    newNCFY = "NCFY"  # the new, number version I want to create         

    arcpy.AddField_management(fc,newNCT,"DOUBLE")
    arcpy.AddField_management(fc,newNCFY,"DOUBLE")

    cursor = arcpy.UpdateCursor(fc)
    for row in cursor:
        row.setValue(newNCT, row.getValue(Decimal(strNCT)))
        row.setValue(newNCFY, row.getValue(Decimal(strNCFY)))
        cursor.updateRow(row)

Error mesage:

Runtime error Traceback (most recent call last): File "",
line 23, in File "C:\Python27\ArcGIS10.1\Lib\decimal.py",
line 548, in new
"Invalid literal for Decimal: %r" % value) File "C:\Python27\ArcGIS10.1\Lib\decimal.py", line 3844, in _raise_error
raise error(explanation) InvalidOperation: Invalid literal for Decimal: 'N_CT'

Best Answer

I would simplify things a bit and convert your string to float using float(). I used the new cursor style, which you may prefer. Please test this "untested" script on a sample dataset before going live.


import arcpy, os
from arcpy import env

arcpy.env.overwriteOutput = True

env.workspace = "C:\Users\OuelletteMS\Desktop\Ice_Data_testarea"

listFCs = arcpy.ListFeatureClasses("*")
for fc in listFCs:
    print str("processing " + fc) # displays the file that is currently being handled

    strNCT = "N_CT"   # the current, text version of the field
    newNCT = "NCT"    # the new, number version I want to create
    strNCFY = "N_CFY" # the current, text version of the field
    newNCFY = "NCFY"  # the new, number version I want to create

    arcpy.AddField_management(fc,newNCT,"DOUBLE")
    arcpy.AddField_management(fc,newNCFY,"DOUBLE")

    with arcpy.da.UpdateCursor(fc, ["N_CT", "NCT", "N_CFY", "NCFY"]) as cursor:
        for row in cursor:
            row[1] = round(float(row[0]), 1) # Make sure there is one decimal place
            row[3] = round(float(row[2]), 1) # Make sure there is one decimal place
            cursor.updateRow(row)
Related Question