how to use a variable as an object property in python

724
5
05-05-2011 08:52 AM
lukestudden
New Contributor
Hi all,

I am using python (9.3) to automate the creation of numerous Featureclasses. Along the way I am adding a new column (field) to each which the field name is essentially a variable as it is all data driven. I am then using the UpdateCursor to update the value of the field with the value of another within the same Feature class. The problem is that since the field name I have added is a variable, I can not use the update cursor to refer to it as it changes after each loop iteration. Is there any way you can use a variable as an objects property, eg.

instead of explicitly stating the field to be updated eg. updaterow1.car1 I would like to use a variable eg: updaterow1.carnumber

Carnumber would then change after each iteration eg. car1, car2, car3 etc.

Is this possible?

Thanks!
Tags (2)
0 Kudos
5 Replies
LoganPugh
Occasional Contributor III
See Accessing Data Using Cursors. Particularly the setValue and getValue methods.
0 Kudos
lukestudden
New Contributor
Hi Logan,

thanks for the reply. I am still struggling to see this. Could you shed any more light on this at all? I would be most grateful! Below is a simplified version of what I have;

Thanks

fieldname = "car "+ str(incrementingcounter)

updaterows1 = gp.updatecursor(outputpoly1)
updaterow1 = updaterows1.next()

    while updaterow1: # selects the entire row, but row has properties hence .fieldname
       
        #### i essentially need this to be something like updaterow1.fieldname
        updaterow1.count_0 = updaterow1.Join_Count

        updaterows1.updaterow(updaterow1)
        updaterow1 = updaterows1.next()

del updaterows1
0 Kudos
LoganPugh
Occasional Contributor III
There are two basic ways to get and set field values on a row:

    * Using the field name, as in value = row.road_type
    * Using getValue and setValue, as in value = row.getValue("road_type")

Try using the getValue and setValue methods instead of using field names as properties.
0 Kudos
lukestudden
New Contributor
Does that mean i can use:

Row.getvalue(fieldname)

As a direct replacement of what i am currently using?

Updaterow1.car1
0 Kudos
LoganPugh
Occasional Contributor III
Yes, they work the same way except that they allow you to pass a variable for the field name instead of hardcoding it.
0 Kudos