Select to view content in your preferred language

AddMessage question

3224
16
Jump to solution
05-24-2013 01:03 PM
AmyKlug
Frequent Contributor
How would you do this using AddMessage instead of print and in a script tool? My code has a parameter and where_clause in the search cursor so the user inputs a name and I would like a message listing field values.

ESRI expample:

import arcpy # Open a searchcursor #  Input: C:/Data/Counties.shp #  Fields: NAME; STATE_NAME; POP2000 #  Sort fields: STATE_NAME A; POP2000 D rows = arcpy.SearchCursor("c:/data/counties.shp",                           fields="NAME; STATE_NAME; POP2000",                           sort_fields="STATE_NAME A; POP2000 D")  # Iterate through the rows in the cursor and print out the # state name, county and population of each. for row in rows:     print("State: {0}, County: {1}, Population: {2}".format(         row.getValue("STATE_NAME"),         row.getValue("NAME"),         row.getValue("POP2000")))
Tags (2)
0 Kudos
16 Replies
T__WayneWhitley
Honored Contributor
Are you showing the dialog box window messages?  There's a checkbox you can check on or off to show the messages...that's where they print to so I suspect you're simply not displaying it.  ...also do you know where to look at the Results panel?  You may optionally look at messages for each run and see any you have printed (along with error messages, if any, etc).
0 Kudos
AmyKlug
Frequent Contributor
I've been viewing message in the results window..................when the message shows up. When I indent arcpy.AddMessage under "for row in rows" it just lets me know that the tool completed. No message
0 Kudos
T__WayneWhitley
Honored Contributor
Do you know you just said 'arcpy.GetMessage'... that is totally different!
0 Kudos
AmyKlug
Frequent Contributor
Sorry...type o. fixed
0 Kudos
T__WayneWhitley
Honored Contributor
Have you set up the tool to accept your input parameter for the query you are trying to apply?

...ahh, I see a problem with your query:
"LINE_ID = 'LID'"

LID is supposed to be a variable, not the text 'LID', understand?
0 Kudos
T__WayneWhitley
Honored Contributor
This is kind of an important detail, easy to overlook, so I'm posting once more to clarify (hopefully).

Since this was a file gdb (as hard-coded in the script) and using GetParameterAsText for the var LID to 'filter' the search cursor, this query should work:

rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", "LINE_ID = '" + LID + "'")


There's more than 1 way to enter a query, but basically from the webhelp topic, Specifying a Query in Python (http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001r000000), a better recommended approach is something like:

qry = """%s = '%s'""" % (arcpy.AddFieldDelimiters(fc, "LINE_ID"), LID)
rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", qry)

Hope that at least sums up better this rather lengthy thread --- for those still unsure what was happening, the incorrect query ("LINE_ID = 'LID'") was returning an empty set hence no messages...nothing to print.

Enjoy,
Wayne
0 Kudos
AmyKlug
Frequent Contributor
Worked like a charm. Muchas Gracias. Looks like I need to learn more about queries!
0 Kudos