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?

asked Sep 3, 2018 at 15:03
2
  • 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. Commented Sep 3, 2018 at 15:16
  • 1
    I dont understand what you are trying to do. Could you add a screenshot showing part of the table? Commented Sep 3, 2018 at 16:28

1 Answer 1

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)
answered Sep 4, 2018 at 7:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.