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

2655
7
Jump to solution
02-05-2016 10:45 PM
MichaelKowalczyk
New Contributor III

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
Regular Contributor III

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 Frequent Contributor

I normally do more complex stuff like this in python.

So, could you show us your code?

MichaelKowalczyk
New Contributor III

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

WesMiller
Regular Contributor III

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
New Contributor III

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
Regular Contributor III

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
New Contributor III

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 Frequent Contributor

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