1

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'

1

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)
  • Thanks for the quick and thourough response. Running this code produces "ValueError: could not convert string to float:" I was incorrect when I said there was no placeholder in empty cells. There is a single space in every cell that does not have a value. Is there a way to either delete these spaces, or skip them? – markouellette Apr 23 '15 at 17:45
  • Maybe you could do something like if row[0] != " ": # Do something – Aaron Apr 23 '15 at 18:06
  • 1
    Thanks! I couldn't get it to match on the space, but I uesd length >1 for the comparison and it worked brilliantly. if len(row[0])>1: row[1] = round(float(row[0]), 1) – markouellette Apr 23 '15 at 19:29
1

You are using Decimal against the field name, not the value. Try row.setValue(newNCT, Decimal(row.getValue(strNCT)))

  • Thanks. I'd vote this answer up, but I don't have the reputation points for that yet. – markouellette Apr 24 '15 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.