Python in model builder...how do I output a table?

1744
8
08-22-2011 10:59 AM
SeanCook
New Contributor
Hello all. I am having trouble figuring out how to return a table from a python script in model builder. I want to take a table view in and output a recordset. I was hoping something like the below would work, but it does not.

table = arcpy.GetParameter(0)
result = table
optable = result.getOutput(0)


If anyone could tell me the magic line that will let me get the above to work, I would greatly appreciate it.

I do intend to modify the table later, but right now inputting and outputting an identical table would be a huge step forward.

Thanks in advance to whoever reads this!
0 Kudos
8 Replies
DaleHoneycutt
Occasional Contributor III
See the help topic Setting script tool parameters. It appears you want a derived output parameter, so pay particular attention to the write-ups about derived output parameters.
0 Kudos
SeanCook
New Contributor
See the help topic Setting script tool parameters. It appears you want a derived output parameter, so pay particular attention to the write-ups about derived output parameters.



For right now, I just want to take a table in, put a table out, and display it. I have read all the documentation, I just can't find anything that works. The getOuput seems to error because, perhaps, it is not the output of a tool. Ok, so....how do I assign a variable as an output?

Any advice on how to save my modified lists into a file I can output as a recordset will also be helpful, but is a problem for later.
0 Kudos
KevinHibma
Esri Regular Contributor
I'm not sure this is exactly what you want to do, but try this code:
import arcpy
inTable = arcpy.GetParameter(0)
arcpy.SetParameter(1, inTable)


Maybe I'm confused why you're casting something to something else? If you want to inspect the attributes of the featureclass you can do it the same if its features or a table (recordset). I assume you'll be using a search cursor to loop through the attributes?

And can you post a link to the doc where this isn't working? If you're using:
table = arcpy.GetParameter(0)
result = table
optable = result.getOutput(0)

Result should be the result of a GP operation....
result = arcpy.TableToTable_conversion('inputTable','in_memory','outputTable)
optable = result.getOutput(0)
0 Kudos
SeanCook
New Contributor
I'm not sure this is exactly what you want to do, but try this code:
import arcpy
inTable = arcpy.GetParameter(0)
arcpy.SetParameter(1, inTable)


Maybe I'm confused why you're casting something to something else? If you want to inspect the attributes of the featureclass you can do it the same if its features or a table (recordset). I assume you'll be using a search cursor to loop through the attributes?

And can you post a link to the doc where this isn't working? If you're using:
table = arcpy.GetParameter(0)
result = table
optable = result.getOutput(0)

Result should be the result of a GP operation....
result = arcpy.TableToTable_conversion('inputTable','in_memory','outputTable)
optable = result.getOutput(0)


Ok, I am working with arcserver, and my only is to be able to do whatever I want with a table....preferably by using a search cursor to feed it into lists, modify the values, and then output a recordset which can be displayed in arcserver. 

I will play with what you've put up for a bit a post again. I really do appreciate the help.

How would you turn a list of lists into a recordset? Write it to a csv, then save that using table to table, then output that derived table?

I was casting something into something else because I couldn't figure out what the results method did.
0 Kudos
KevinHibma
Esri Regular Contributor
Ok, I am working with arcserver, and my only goal is to be able to do whatever I want with a table....preferably by using a search cursor to feed it into lists, modify the values, and then output a recordset which can be displayed in arcserver.

I will play with what you've put up for a bit a post again. I really do appreciate the help.


Right - I wouldn't worry about the conversion then...just use the cursor right off the input features. Do you thing, then write out the new table. Here's some untested code off the top of my head

import arcpy

inTable = arcpy.GetParameterAsText(0)

SearchRows = arcpy.SearchCursor(inTable)

outTable = r'in_memory/OuTable'
outRows = arcpy.InsertCursor(outTable)

for row in SearchRows:
        value1 = row.NAME        
        value2 = row.TYPE
        value3 = row.SUPERFIELD
        
        singleOutRow = outRows.newRow()
        singleOutRow.outNAME = value1
        singleOutRow.outTYPE = value2
        singleOutRow.outSUPERFIELD = value3
        outRows.insertRow(singleOutRow)
               
arcpy.SetParameter(1, outTable)
0 Kudos
SeanCook
New Contributor
Right - I wouldn't worry about the conversion then...just use the cursor right off the input features. Do you thing, then write out the new table. Here's some untested code off the top of my head

import arcpy

inTable = arcpy.GetParameterAsText(0)

SearchRows = arcpy.SearchCursor(inTable)

outTable = r'in_memory/OuTable'
outRows = arcpy.InsertCursor(outTable)

for row in SearchRows:
        value1 = row.NAME        
        value2 = row.TYPE
        value3 = row.SUPERFIELD
        
        singleOutRow = outRows.newRow()
        singleOutRow.outNAME = value1
        singleOutRow.outTYPE = value2
        singleOutRow.outSUPERFIELD = value3
        outRows.insertRow(singleOutRow)
               
arcpy.SetParameter(1, outTable)



Well, I might want to transpose rows and fields, etc, not just modify values. I am going to be creating a substantially different table than what I am inputting. Also editing field names, deleting fields, adding fields, moving fields, etc. It's going to get extremely slow using arc tools.
0 Kudos
SeanCook
New Contributor
Kevin, you have been a huge help. I got it working for now....thanks!
0 Kudos
SumitSharma
New Contributor III
Hi khibma,

I have a geoprocessing service �??A�?� that returns output as GPRecordset.   Now I want to use the returned recordset  as input into another GP service �??B�?� that is created using python script.

I am using following to set input type inside the python script for service �??B�?�:

Arcpy.GetParameter(0)

And from Script side (Gp properties) I am setting input type  �??Recordset�?� as parameter type. Attached is the screenshot.

But when I try to run the service �??B�?�, it fails as it is not allowing passing record set as input.

The workflow I am trying to accomplish is generating tables from GP service �??A�?� and passing that tables (as recordset) to GP service �??B�?� for reporting. I am struggling with passing the data to GP service �??B�?�.

I would highly appreciate any help in this regard.

FYI: Working with ARCGIS Server 10.0.

Thanks,
Sumit
0 Kudos