Python Scrip that Copies Data in a Field to another Field

Discussion created by ianleinwand on Apr 30, 2012
Latest reply on Apr 30, 2012 by dkwiens
I'm trying to copy data from one field to another field within a loop. The issue I'm having is how to define the field from which data is being copied as a
variable. The field name from which I want to copy data changes with each iteration of the loop based on the input datasets name.

Here is the code and I highlighted where I'm having trouble... I either get an error or the data does not copy but the script runs... depends on how I s
specify the field name variable.

See... arcpy.CalculateField_managment section of the code

# ---------------------------------------------------------------------------
# combine_ecoregion_tree_species.py
# Created on: 2012-04-25 09:17:22.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
from arcpy import env

# Check out any necessary licenses
arcpy.env.overwriteOutput = True

# Set Workspace
arcpy.env.workspace = r"\\Sxftcsls001\m\SDL\Data\Forest_Parameters\FHTET\240m\20111019_L48"

rasterList = arcpy.ListRasters("*", "GRID")
listCount = len(rasterList)
print listCount

for r in rasterList:
if(r.startswith ("f")):
print r

# Local variables:
OutWorkspace = "D:\\Risk_Models\\Ecoregion_Tree_Species\\"
eco_id = "D:\\Risk_Models\\Ecoregion_Tree_Species\\eco_id"
Extent = "-2370945 255315 2273055 3189315"
snap = "\\\\Sxftcsls001\\m\\SDL\\Data\\Snap_Grids\\FHTET\\240m\\L48\\snap"
species_pa = OutWorkspace + r
eco_pa = OutWorkspace + "eco_" + r
species_field = str(r)

print r
print " species code is " + species_field

# Process: Con
arcpy.env.snapRaster = snap
arcpy.env.extent = Extent
arcpy.gp.Con_sa(r, 1, species_pa, 0, "\"VALUE\" > 0")

# Process: Combine
arcpy.gp.Combine_sa([species_pa,eco_id], eco_pa)

print "combine complete"

# Process: Add Field
arcpy.AddField_management(eco_pa, "SPECIES", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(eco_pa, "OCCUR", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

print "add field"

arcpy.CalculateField_management(eco_pa, "SPECIES", "species_field", "PYTHON")

print "Calculate Species Field"

fields = arcpy.ListFields(eco_pa)
fieldnames = [f.name for f in fields]

fname = fieldnames[3]
print fname

fname_calc = [str(fname)]

print fname_calc

print fieldnames

arcpy.CalculateField_management(eco_pa, "OCCUR", "%s" %fname_calc, "PYTHON")

print "calculated Field"