Select to view content in your preferred language

Python Scrip that Copies Data in a Field to another Field

5563
5
04-30-2012 08:23 AM
IanLeinwand
Emerging Contributor
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.CheckOutExtension("spatial")
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"
Tags (2)
0 Kudos
5 Replies
DarrenWiens2
MVP Alum
You need to wrap field names in !'s when they are used in the field calculator expression.

arcpy.CalculateField_management(eco_pa, "OCCUR", "!" + fname_calc + "!", "PYTHON")
 

Tip: make your calculation in the ArcMap field calculator and look at the results to see the proper syntax.
0 Kudos
IanLeinwand
Emerging Contributor
You need to wrap field names in !'s when they are used in the field calculator expression.

arcpy.CalculateField_management(eco_pa, "OCCUR", "!" + fname_calc + "!", "PYTHON")
 

Tip: make your calculation in the ArcMap field calculator and look at the results to see the proper syntax.


Thanks... I'm trying that now...

I created a model in model builder with the calculate field tool set do do the calculation I wanted and exported that to python... it did not give me the
correct syntax. It had the field wrapped in [ ]

thanks for your response... I'll let you know how it goes.
0 Kudos
IanLeinwand
Emerging Contributor
Just tried that and got an error saying it can't concatenate string and list objects.
0 Kudos
IanLeinwand
Emerging Contributor
awsome... this worked... arcpy.CalculateField_management(eco_pa, "OCCUR", "!" + fname + "!", "PYTHON")

it didn't like reading it in as a string...

thanks

Ian
0 Kudos
DarrenWiens2
MVP Alum
Ah, yeah. I'm not sure what it's trying to accomplish with "fname_calc = [str(fname)]". What I am sure about is this: the script produced by exporting from ModelBuilder is a lie.
0 Kudos