0

I am trying to fill an empty column with unique number for every data, I use the following code:

import arcpy
my_input = []
column = "REGION_ID"
column1 = "MUNICIPAL_ID"
column2 = "INSTR_ID"
column3 = "DISTRICT_ID"
searchcursor = arcpy.SearchCursor("house_point")
fields = ['ind']
index = 1
cursor = arcpy.da.InsertCursor('D:/yvareli.mdb/house_point', fields)

for row in searchcursor:
    my_input.append(int(str(row.getValue(column)) + str(row.getValue(column1)) + str(row.getValue(column2)) + str(row.getValue(column3))))


for i in range(len(my_input)):
    cursor.insertRow([index])
    index += 1
del cursor

but instead of updating existing row, it just creates empty copies for every column and adds unique values in new rows.

6
  • 1
    You left out the da in your SearchCursor and used InsertCursor not UpdateCursor.
    – Vince
    Jun 17 at 13:48
  • Are you using ArcGIS Pro or ArcMap?
    – PolyGeo
    Jun 17 at 20:33
  • What happens when you run the code that you have presented?
    – PolyGeo
    Jun 17 at 20:44
  • I'm using arcmap
    – sandrop
    Jun 18 at 8:00
  • When I run this code, instead to update row, it creates new one and as I have 30000 recording, after executing the script, it will be 60000
    – sandrop
    Jun 18 at 8:02

1 Answer 1

0

You're using InsertCursor that's why it's inserting new rows. Try using UpdateCursor.

import arcpy
column = "REGION_ID"
column1 = "MUNICIPAL_ID"
column2 = "INSTR_ID"
column3 = "DISTRICT_ID"
ind = 'ind'
fields = [column, column1, column2, column3, ind]

cursor = arcpy.da.UpdateCursor('D:/yvareli.mdb/house_point', fields)

for row in cursor:
    value = int(str(row[0]) + str(row[1]) + str(row[2]) + str(row[3])))
    row[-1] = value
    cursor.updateRow(row)


del cursor
2
  • Thank you, worked, god bless you
    – sandrop
    Jun 21 at 13:50
  • You're welcome. You can mark it as a correct answer.
    – Ahmad Raji
    Jun 21 at 13:56

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.