Fetching row using python script.

3606
10
Jump to solution
09-14-2015 01:52 AM
RajP
by
New Contributor

Is there any possible fetching row using Python script?

0 Kudos
1 Solution

Accepted Solutions
MahtabAlam1
Occasional Contributor

If I understand correctly you could use following script:

cursor = arcpy.UpdateCursor("testFC")
gridDict = {}


for row in cursor:
    print row.getValue("GridNo")
    gridNo = row.getValue("GridNo")


    if(gridDict.has_key(gridNo)):
        gridDict[gridNo] = gridDict[gridNo] + 1
    else:
        gridDict[gridNo] = 1


    name = "{0}_{1}".format(gridNo, str(gridDict[gridNo]).zfill(3))
    row.setValue("Name",name)
    cursor.updateRow(row)

Make sure to correct the feature class path and field names as they are.

Thanks,

Mahtab

View solution in original post

10 Replies
DanPatterson_Retired
MVP Emeritus

This is the direct link to "searchcursor"

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/searchcursor-class.htm

There is a whole of lot of reading in the arcpy module, its classes and functions which should be read to put this in context.

RajP
by
New Contributor

The last link is useful. But it is not clear my problem. I have attribute table that have Name and Grid no field. I have to update Name field using Grid no. For Example in the Grid Field (Grid1, Grid2, Gird2, Grid1, Grid1) so I have update Name field like as (Grid1_001, Grid2_001, Grid2_002, Grid1_002, Grid1_003). Kindly suggest me.

0 Kudos
WesMiller
Regular Contributor III

Try searching for arcpy.da.UpdateCursor that will get you where you want to go

0 Kudos
NeilAyres
MVP Alum

If you have another column which contains the "001", "002" or perhaps just the number, you could probably just do a concatenation with the field calculator.

If you havn't got this second field, what governs the "001", "002" part connecting to "Grid1" etc?

RajP
by
New Contributor

There is no column which contains the "001", "002".

0 Kudos
DanPatterson_Retired
MVP Emeritus

Show the fields you have.  It is not clear whether you want to add new fields, or change the name of existing fields.  In both cases, emulate the code in.

Add

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/add-field.htm

Alter/Rename

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/alter-field-properties.htm

0 Kudos
MahtabAlam1
Occasional Contributor

If I understand correctly you could use following script:

cursor = arcpy.UpdateCursor("testFC")
gridDict = {}


for row in cursor:
    print row.getValue("GridNo")
    gridNo = row.getValue("GridNo")


    if(gridDict.has_key(gridNo)):
        gridDict[gridNo] = gridDict[gridNo] + 1
    else:
        gridDict[gridNo] = 1


    name = "{0}_{1}".format(gridNo, str(gridDict[gridNo]).zfill(3))
    row.setValue("Name",name)
    cursor.updateRow(row)

Make sure to correct the feature class path and field names as they are.

Thanks,

Mahtab

RajP
by
New Contributor

Thanking you so much. It was very useful.

I had created the Python toolbox. Is there any possible to hide our source in the Python toolbox.

0 Kudos
MahtabAlam1
Occasional Contributor

I am afraid you can't do this. Search for python obfuscation and you will get the answer.

0 Kudos