Defining a variable for field name in python

5672
6
Jump to solution
07-20-2015 07:07 AM
GyeyoungChoi
New Contributor II

I have a code looks like this

rows = arcpy.UpdateCursor(featureClass)

row = rows.next()

#AddressFieldName = "ADD_FULL"

while row:

  s= row.ADD_FULL

I'd like to use the field name as variable but if I wan to try like:

s=row. + AddressFieldName

or such thing that I can think of won't work.

Is there any way that I can use it as variable?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Frequent Contributor

Wes Miller is right you should look into using the faster da cursor. If you want to continue using the cursor you are using then rather than accessing the field name directly as you have use this approach:

myField = "ADD_FULL" # You can change this to what ever field name you want

rows = arcpy.UpdateCursor(featureClass)  

row = rows.next()  

while row:  

  s= row.getValue(myField)

  print s

  row = rows.next()

View solution in original post

0 Kudos
6 Replies
WesMiller
Regular Contributor III

Try using the da cursor it's easier to understand.

edit:

For your current code on line 5 s will now hold the attribute from your "ADD_FULL" field of the first record in your feature class table.

if you add the below it should print the attribute of every record in your feature class

rows = arcpy.UpdateCursor(featureClass)  

row = rows.next() 

#AddressFieldName = "ADD_FULL" 

while row: 

  s= row.ADD_FULL

  print s

  row = rows.next()

0 Kudos
GyeyoungChoi
New Contributor II

Wes,

I was trying to get the field name in separate variable.

so I can received different field name from toolbox.

0 Kudos
WesMiller
Regular Contributor III

I don't understand what your after did you want to move the data to another field or maybe use another field to manipulate the data? To get another field in the method your using you would just do x = row.someotherfield then s+x would equal the values of s and x. If you give me more details or an example I could help you better.

0 Kudos
GyeyoungChoi
New Contributor II

I think I explained it wrong.

So, simply I was trying to use a variable to indicate a name of filed (column name)

define,

AddressFieldName = "ADD_FIELD"

then that field name has to be in

variable name s

but originally it works if I declare as (field[column] name ADD_FIELD)

s=row.ADD_FIELD

however I want to use ADD_FIELD  as variable

s=row+ "." + AddressFieldName

this returns error

or any other ways that I can think of.

0 Kudos
DuncanHornby
MVP Frequent Contributor

Wes Miller is right you should look into using the faster da cursor. If you want to continue using the cursor you are using then rather than accessing the field name directly as you have use this approach:

myField = "ADD_FULL" # You can change this to what ever field name you want

rows = arcpy.UpdateCursor(featureClass)  

row = rows.next()  

while row:  

  s= row.getValue(myField)

  print s

  row = rows.next()

View solution in original post

0 Kudos
GyeyoungChoi
New Contributor II

Duncan,

almost miss your reply

I'll try it.

Thanks

0 Kudos