1

I got a table with the name Predios

like this:

OBJECTID   PRM_IDENTIFICA

1          4030200606130

2          403020060615

3          40302006011700

Can I use search cursor to find the records when the PRM_IDENTIFICA len its <> 14.???

Right now i got:

import arcpy

tabla="C:/Users/jaguero/Desktop/validacion tabla/Valid.gdb/Predios"

rows=arcpy.SearchCursor(tabla)

row=rows.next()

while row:

print len(row.PRM_IDENTIFICA)

row=rows.next()

giving me this:

13

12

14

What if I want only as a result the records when the len is <> from 14 ?

  • How does your top example (1 4030200606130 etc.) relate to your bottom example (13 etc.)? – Midavalo May 20 '16 at 20:53
  • Try select by attribute using char_length function. If it works use query with your search cursor. For v 10 I suggest much easier to use da.SearchCursor – FelixIP May 20 '16 at 21:34
  • 1
    Use arcpy.da.SearchCursor -- much faster and more Pythonic – Vince May 20 '16 at 21:51
2

For the old style cursor that you are using I would use this code:

import arcpy

tabla="C:/Users/jaguero/Desktop/validacion tabla/Valid.gdb/Predios"

rows=arcpy.SearchCursor(tabla)

for row in rows:
    if len(row.PRM_IDENTIFICA) != 14: #new code
        print len(row.PRM_IDENTIFICA)

del row,rows

However, I strongly recommend using Data Access cursors if you are using ArcGIS 10.1 for Desktop or later.

0

I would use a simple if statement to skip values that equal 14.

import arcpy

tabla="C:/Users/jaguero/Desktop/validacion tabla/Valid.gdb/Predios"

rows=arcpy.SearchCursor(tabla)

row=rows.next()

while row:

if len(row.PRM_IDENTIFICA) != 14: #new code
    print len(row.PRM_IDENTIFICA)

row=rows.next()
0

I have a layer (shapefile) with 37 features in it sitting on the top of my Table of Content. It has text field "NAME". Every odd record populated by 3 characters long string, other records populated by 2 characters long string.

I tested if CHAR_LENGTH function is valid one for query definition using this script:

mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd)[0]
table=arcpy.da.TableToNumPyArray(layer,"NAME")
arcpy.AddMessage(len(table))
table=arcpy.da.TableToNumPyArray(layer,"NAME",r'CHAR_LENGTH("NAME")=3')
arcpy.AddMessage(len(table))

Output:

  • Running script Script4...
  • 37
  • 19
  • Completed script Script4...

It means that function can be used in cursor(s) definition queries

PERFORMACE TEST using:

try: arcpy
except: import arcpy
import time
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd)[0]
n=25000

t0=time.time()
for i in range(n):
    table=[]
    for row in arcpy.da.SearchCursor(layer,"NAME"):
        if len(row[0])==3:table.append(row)
arcpy.AddMessage ('%i seconds for iterations' %(time.time()-t0))

t0=time.time()
for i in range(n):
    table=[row for row in arcpy.da.SearchCursor(layer,"NAME",r'CHAR_LENGTH("NAME")=3')]
arcpy.AddMessage ('%i seconds for cursor' %(time.time()-t0))

t0=time.time()
for i in range(n):
    table=arcpy.da.TableToNumPyArray(layer,"NAME",r'CHAR_LENGTH("NAME")=3')
arcpy.AddMessage ('%i seconds for numpy' %(time.time()-t0))

On the same dataset and my out of date home PC

  • 22 seconds for iterations
  • 24 seconds for cursor
  • 25 seconds for numpy

I admit defeat, query imposed on dataset works slower than simple filter during iterations. Makes it look smart though...

  • This may be right, but I don't have time to test. However, I think it departs too much from the workflow being asked about to be of much use as an answer to this question. If you were showing how to use CHAR_LENGTH with a cursor then I would be more interested in testing it but the inclusion of arcpy.da.TableToNumPyArray just seems out of place when looking for an MCVE aligned to the question. – PolyGeo May 21 '16 at 2:04
  • SearchCursor is identical to table.., the only difference being it works faster and faster unable to deal with geometry field – FelixIP May 21 '16 at 3:12
  • I think some performance timing between using this method to print the results and the same results from arcpy.da.SearchCursor() on a million rows would be interesting. If there is s big gap in favour of your method then it could make a more compelling case to use it. – PolyGeo May 21 '16 at 4:52
  • Well, to me the essence of Q is about ability to use string manipulation function in search cursor. My test proves it is possible. As far as timing goes I wouldn't use table method on 1 mil records due to memory concerns. I ll use above script see which one is faster by repeat readings – FelixIP May 21 '16 at 5:48

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.