0

I have a feature Class with the columns KOMMUNE and GKR and some other columns.. Both columns are string. In the column KOMMUNE I have the value 0723 and 0724. In column GKR I have the value 0107 and 0108. My problem is: If column KOMMUNE has the value 0723 i will add 2000 to the value 0107 in the colum GKR so the value becomes 2107 and if the value in KOMMUNE is 0724 I will add 3000 to the value 0108 in the colum GKR so the value becomes 3108.

Have tried with arcpy.CalculateField_management but I can not select the values ​​with it.

import sys, string, os, arcpy

from arcpy import env

env.workspace = r"C:\Tom\ArcPyTest\Slå sammen kommuner"

#Kommune_1 = arcpy.GetParameterAsText(0)

#Kommune_2 = arcpy.GetParameterAsText(1)

#Nytt_Kommune_Nr = arcpy.GetParameterAsText(2)



Kommune_1 = 723

Kommune_2 = 720

Nytt_Kommune_Nr = "0722"

FcKommune = r"C:\Tom\ArcPyTest\Slå sammen kommuner\07_Vestfold inn.gdb\gkr2013_brutto07"

arcpy.env.overwriteOutput = True

arcpy.AddField_management(FcKommune, "KOM", "TEXT", '', '', 4)
arcpy.AddField_management(FcKommune, "GKR", "TEXT", '', '', 4)

arcpy.AddField_management(FcKommune, "KOMMUNE", "TEXT", '', '', 4)
arcpy.CalculateField_management(FcKommune, "KOMMUNE",  "!" + "KOMM" + "!", "PYTHON")



rows = arcpy.da.UpdateCursor(FcKommune, ["KOMMUNE", "GRUNNKRETS", "KOM", "GKR"])

for row in rows:
    if (row[0]) == str(Kommune_1):
        row[2] = str(Nytt_Kommune_Nr)
    if (row[0]) == str(Kommune_1):
        row[3] = (row[1] [4:8])
    if (row[0]) == str(Kommune_2):
        row[2] = str(Nytt_Kommune_Nr)
    if (row[0]) == str(Kommune_2):
        row[3] = (row[1] [4:8])

    if (row[0]) == str(Kommune_1):
        arcpy.CalculateField_management(FcKommune, "GKR", "[GKR] +2000" , "VB", "")
    if (row[0]) == str(Kommune_2):
        arcpy.CalculateField_management(FcKommune, "GKR", "[GKR] +3000" , "VB", "")

    rows.updateRow(row)
1

I have tested "[GKR]+2000" with the CalculateField tool in ArcToolbox. It works well, even if GKR is a text field. Maybe you could try without the space between [GKR] and +2000.

  • The expression works fine for me to. But the arcpy.calculateField_management don't let me select the field KOMMUNE in a for loop. – user15888 Feb 26 '14 at 11:36
0

You use CalcuateField_management for an entire feature layer, not one row at a time... try replacing your "for row in rows:" section...

for row in rows:
    if (row[0]) == str(Kommune_1):
        row[2] = str(Nytt_Kommune_Nr)
        row[3] = (row[1] [4:8])
        row[3] = str(int(row[3]) + 2000)

    if (row[0]) == str(Kommune_2):
        row[2] = str(Nytt_Kommune_Nr)
        row[3] = (row[1] [4:8])
        row[3] = str(int(row[3]) + 3000)

    rows.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.