Select to view content in your preferred language

How to get field values from every feature, and place them in a comma seperated string?

3558
7
Jump to solution
02-05-2016 10:45 PM
MichaelKowalczyk
Deactivated User

Hello, I have a modelbuilder tool with an iterator that produces a selection in another feature class each iteration.  I would like to get the value of a field from the records in each selection, and put them in a coma separated string.

I have attempted to use an embedded python tool to use a search cursor over these records, but it does not seem to work.  I get a runtime error: cannot open '<<field name>>'

Do you have any suggestions as to how to accomplish this?  Would a searchcursor work in an iterator model? Thank you.

--

My python tool containing the search cursor.  It works as expected in isolation, but not in the model builder tool.

import arcpy
from arcpy import env
env.workspace = arcpy.GetParameterAsText(0) 

##import time
#from datetime import datetime

##current_time=time.strftime("%H:%M:%S")+" "+ time.strftime("%d/%m/%Y")


local_pairlist = "" #list will be populated with pair ids from paths in prevailing direction

#selected features

pathfc      = arcpy.GetParameterAsText(1)  #demand paths in prevailing direction

path_pair_id= str(arcpy.GetParameterAsText(2)) #pair id field


with arcpy.da.SearchCursor(pathfc,(path_pair_id,)) as path_cursor:


                
        newpair=path_row[0] 

        if local_pairlist == "":
            local_pairlist+=newpair
        else:
            local_pairlist+=r","+newpair
       
                
    #--//colection loop--#   

##print "OD Builder Finished!\n\nOutput table="+str(out_name)+"\n\nLocation: " + str(out_path)


arcpy.AddMessage("Subpairs saved!")
arcpy.SetParameter(3, local_pairlist) 
0 Kudos
1 Solution

Accepted Solutions
WesMiller
Deactivated User

Yes a search cursor will work in a model with an iterator in it.

Here's a picture of the model i tested with:

Here's the code used:

import arcpy


#Variables
fc = arcpy.GetParameterAsText(0)
fld = arcpy.GetParameterAsText(1)


mystring = ""
with arcpy.da.SearchCursor(fc,fld) as cursor:
    for row in cursor:
        mystring += str(row[0]) +","


arcpy.AddMessage(mystring[:-1])

View solution in original post

7 Replies
NeilAyres
MVP Alum

I normally do more complex stuff like this in python.

So, could you show us your code?

MichaelKowalczyk
Deactivated User

I actually ended up rewriting the tool from scratch in Python, and actually got at least a 10 fold improvement in performance!

WesMiller
Deactivated User

Yes a search cursor will work in a model with an iterator in it.

Here's a picture of the model i tested with:

Here's the code used:

import arcpy


#Variables
fc = arcpy.GetParameterAsText(0)
fld = arcpy.GetParameterAsText(1)


mystring = ""
with arcpy.da.SearchCursor(fc,fld) as cursor:
    for row in cursor:
        mystring += str(row[0]) +","


arcpy.AddMessage(mystring[:-1])
MichaelKowalczyk
Deactivated User

Thank you, , for confirming that it should work.  My tool looked very much like yours.  However, I was getting an error that it could not open the specified field.  Do you have any suggestions?

0 Kudos
WesMiller
Deactivated User

Could you post the error you are getting. Have you confirmed the field name exists? Could you also post the model you are using

0 Kudos
MichaelKowalczyk
Deactivated User

I was using an unwieldy model that I should have written in arcpy to begin with.  I actually managed to do so in a relatively quick manner, it works 10-100 x faster than model builder!  Troubleshooting it seems no longer necessary. 

NeilAyres
MVP Alum

Yes,

there have been other post here about the speed improvements once a task is coded using arcpy / python.

Good to see you have your solution.

0 Kudos