3

I want to figure out how to be able to change values in fields for use in further projects. Right now , I would like to be able to add '-WA' to the existing field value 53. (ie 53-WA). This is a test project as I am trying to teach myself python. The code I got so far runs but the values do not change.

import arcpy, os

arcpy.env.workspace = r'C:\Users\david.fleck\Python.mdb'

fc= 'Parking_Lots_Collected_2015'

field = ['STATEFP']

cursor = arcpy.UpdateCursor(fc,field)

with arcpy.da.UpdateCursor(fc,field)as cursor:

    for row in cursor:
        if row== 53:
            row.setValue(row.getValue(field) + '-WA')
            cursor.updateRow(row)

print "Finished"

I have also tried row ==''53 + '-CA' instead of row.setvalue but still no change.

  • @Barbarossa shows a good way, but just for reference, a couple of problems with your code is that you set cursor twice (with statement usually better), and you're missing a space between '(fc, field)' and 'as'. as well as between 'row' and '=='. And if row is a text field, you need quotes around 53. If it's not a text field, you can't append text to it. – recurvata Aug 6 '15 at 18:39
10

Try the following:

import arcpy, os
arcpy.env.workspace = r'C:\Users\david.fleck\Python.mdb'

fc= 'Parking_Lots_Collected_2015'
field = ['STATEFP']

with arcpy.da.UpdateCursor(fc,field) as cursor:
    for row in cursor:
        if row[0] == '53':
            row[0] = row[0] + '-WA'
            cursor.updateRow(row)

print "Finished"

Note that this will only work if STATEFP is a text field.

  • Might need to put the 53 in quotes, assuming it is a text field. – ianbroad Aug 6 '15 at 18:45
  • Correct. See edits. – Barbarossa Aug 6 '15 at 18:47
1

To provide a contrast to @Barbarossa's answer. Here is how it is done using the other arcpy update cursor. I adjusted things a little bit to simplify some python code.

I find when learning/explaining arcpy cursors, to save the da cursors for later after you get a hold of this version. This version is slower and less powerful, but it is pretty simple to grasp.

from arcpy import env

env.workspace = r'C:\Users\david.fleck\Python.mdb'

with arcpy.UpdateCursor('Parking_Lots_Collected_2015') as rows:
  for row in rows:
    if row.STATEFP == '53':
      row.STATEFP += '-WA'
      rows.updateRow(row)

print "Finished"

It seems ESRI has changed some code samples to use the with statement. Since you're learning, you may run across other samples using a different way to accomplish the same thing.

import arcpy

arcpy.env.workspace = r'C:\Users\david.fleck\Python.mdb'

rows = arcpy.UpdateCursor('Parking_Lots_Collected_2015')
for row in rows:
  if row.STATEFP == '53':
    row.STATEFP = row.STATEFP + '53'
    rows.updateRow(row)

del row
del rows
print "Finished"

Hopefully both samples along with @Barbarossa's answer provide some contrast for you. Being newer with python, if you don't understand some of the different ways things were done here, please comment to get an explanation. Either that or Dr. Google works wonders.

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.