1

I have table with a field containing attribute names in another language (oldname1, oldname2,...). I want to create a new field in which I can add (with field calculator) the translated name of each attribute in my language (newname1,newname1,...).

enter image description here

I thought the cursor would be the best way to realize this. The original field is called fieldA and the new field fieldB

I tried with a cursor but my script isn't working. The error is given for line "if row in 'fieldA'= 'oldname1'

myTable = ":\\path to table"


cursor = arcpy.da.SearchCursor(myTable, ['fieldA', 'fieldB'])
for row in cursor:
    if row in 'fieldA'= "oldname"
    arcpy.CalculateField_management(myTable, 'fieldB',"newname1")
    elif...
    .....

I tried to add cursors for each field as variable before the loop:

fieldA= arcpy.da.SearchCursor(myTable, ['fieldA'])
fieldB= arcpy.da.SearchCursor(myTable, ['fieldB'])

But it is not helping. I also try to write ['fieldA'] instead of 'fieldA'

Can I create variables for each field and use it in the cursor? or do you have an idea of how I could fix this?

  • 1
    You're not using DA Cursors according to the examples in the documentation. It wouldn't be wrong to name the objects fieldAcursor and fieldBcursor, but they are not fieldA and fieldB. It's also wrong to mix-n-match CalculateField with cursors, and your indentation and syntax on the if are wrong. Also the row in is a syntax error, since row is an array. And, if you want to update values, you need an UpdateCursor, not a SearchCursor. – Vince Sep 3 '18 at 15:16
  • 1
    I dont understand what you are trying to do. Could you add a screenshot showing part of the table? – BERA Sep 3 '18 at 16:28
1

You could use the CalculateField function or an UpdateCursor, to name an attribute in a new field as a function of an attribute in another field. Here are 3 examples:

import arcpy

arcpy.env.workspace = "D:/"

# example 1 with a dictonary
expression = "getNewName(!OldName!)"
codeblock = """
def getNewName(oldname):
    my_dictionary = {'Paul_old' : 'Paul_new',
                 'James_old' : 'James_new',
                 'Jane_old' : 'Jane_new'}
    return my_dictionary[oldname]"""

arcpy.CalculateField_management("MyTest.shp", "NewName", 
                                expression, "PYTHON", codeblock)

# example 2 with a string manipulation
expression = "getNewName(!OldName!)"
codeblock = """
def getNewName(oldname):
    return oldname.split('_')[0] + '_new'"""

arcpy.CalculateField_management("MyTest.shp", "NewName", 
                                expression, "PYTHON", codeblock)

# exmaple 3 with update cursor

fields = ['OldName', 'NewName']

with arcpy.da.UpdateCursor("MyTest.shp", fields) as cursor:
    for row in cursor:
        # example with a string manipulation, but could be anything
        # index of row correspond to the index of fields variable
        row[1] = row[0].split('_')[0] + '_new'
        cursor.updateRow(row)

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.