AddMessage question

1786
16
Jump to solution
05-24-2013 01:03 PM
AmyKlug
Occasional Contributor III
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
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent 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

View solution in original post

0 Kudos
16 Replies
T__WayneWhitley
Frequent Contributor
...for 'print' substitute 'arcpy.AddMessage' with the message enclosed in parenthesis.  Just make sure your indention is correct.

As in:
arcpy.AddMessage("State: {0}, County: {1}, Population: {2}".format(row.getValue("STATE_NAME"), row.getValue("NAME"), row.getValue("POP2000")))


I think that will work...test it out.
0 Kudos
AmyKlug
Occasional Contributor III
I did try that but it will run the code but will ignore the "AddMessage"
0 Kudos
T__WayneWhitley
Frequent Contributor
Any error messages?  ...you have to run this as a script tool via ArcGIS's toolbox to 'port' it to it's geoprocessor interface.  You don't have to define any parameters, but must display it's dialog window to see the returned messages - of course if erroring out before you get to the messages you've attempted to add, then you won't see them.

If the message itself is a problem, then try formatting the text before you add it to the AddMessage parameter...debatably easier to read the code that way anyway.

...as in:
theMessage = "State: {0}, County: {1}, Population: {2}".format(row.getValue("STATE_NAME"), row.getValue("NAME"), row.getValue("POP2000"))
arcpy.AddMessage(theMessage)


Again, make sure you don't have an indention error earlier in the code block.  If you're still having problems, include more complete details what you are doing and any feedback messages.
0 Kudos
AmyKlug
Occasional Contributor III
I took your advice and got it to work like this:


import arcpy
LID = arcpy.GetParameterAsText(0)
rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", "LINE_ID = 'LID'")
for row in rows:
    row.getValue(rows)
theMessage = "State: {0}, Name: {1} , Population: {2}".format(
            row.getValue("STATE_NAME"),
            row.getValue("NAME"),
            row.getValue("POP2000"))
arcpy.AddMessage(theMessage)
del rows, LID


I needed to define "theMessage" without indentation and add row.getValue(rows) instead under "for row in rows".

THANK YOU!!!!!!!!!!
0 Kudos
AmyKlug
Occasional Contributor III
I took your advice and got it to work like this:


import arcpy
LID = arcpy.GetParameterAsText(0)
rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", "LINE_ID = 'LID'")
for row in rows:
    row.getValue(rows)
theMessage = "State: {0}, Name: {1} , Population: {2}".format(
            row.getValue("STATE_NAME"),
            row.getValue("NAME"),
            row.getValue("POP2000"))
arcpy.AddMessage(theMessage)
del rows, LID


I needed to define "theMessage" without indentation and add row.getValue(rows) instead under "for row in rows".

THANK YOU!!!!!!!!!!


OK this worked while I was in my other MXD but now since I have opened a new mxd it is telling me the same error that the row.getvalue(State_Name) "row" is not defined. arrrrggggg!

It wont work again in any mxd.

EDIT: I think the reason it was working is because to make sure the fields would print i ran the print code first (see my first post) and that is where it was getting it's "row" definition
0 Kudos
T__WayneWhitley
Frequent Contributor
Amy, that code makes no sense - should fail at:
row.getValue(rows)

...because 'rows' is your cursor object, not a field name -- try this, your code corrected from above - you must have the correct county feature class to run this on:
import arcpy
LID = arcpy.GetParameterAsText(0)
rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", "LINE_ID = 'LID'")

for row in rows:
     theMessage = "State: {0}, Name: {1} , Population: {2}".format(row.getValue("STATE_NAME"), row.getValue("NAME"), row.getValue("POP2000"))
     
        arcpy.AddMessage(theMessage)

del rows, LID


Careful with indention!

Enjoy,
Wayne


EDIT:  Or, if this helps you, the backslash is to Python a 'line continuation' character, meaning what you see in italic text should be a single line of code, expressed below perhaps easier-to-read:
for row in rows:
     theMessage = "State: {0}, Name: {1}, Population: {2}".\
                     format(row.getValue("STATE_NAME"), \
                            row.getValue("NAME"), \
                            row.getValue("POP2000"))        
     arcpy.AddMessage(theMessage)
0 Kudos
AmyKlug
Occasional Contributor III
Whenever I use arcpy.AddMessage indented under "for row in rows" it just gets ignored and no message appears even though the code runs without error

import arcpy
LID = arcpy.GetParameterAsText(0)
rows = arcpy.SearchCursor("C:/Python/Python.gdb/Counties", "LINE_ID = 'LID'")
for row in rows:
       theMessage = "State: {0}, Name: {1} , Population: {2}".format(row.getValue("STATE_NAME"),
row.getValue("NAME"),row.getValue("POP2000"))
       arcpy.AddMessage(theMessage)
del rows, LID


Edit: my code looks different when posted. might be having formatting issues

The "print" version works fine in the python window so I am assuming my feature class is ok.
0 Kudos
T__WayneWhitley
Frequent Contributor
I edited my response above because I was having trouble formatting my message...

Do you have a script tool?  Are you running the code from the toolbox?  Try editing the script tool py you are using, pasting in the portion from above...again, if there's a problem, I need to know what the error messages are.

Of course if not running from ArcToolbox, then of course you won't see the messages.


Hope that helps.
Wayne
0 Kudos
AmyKlug
Occasional Contributor III
I edited my response above because I was having trouble formatting my message...

Do you have a script tool?  Are you running the code from the toolbox?  Try editing the script tool py you are using, pasting in the portion from above...again, if there's a problem, I need to know what the error messages are.

Of course if not running from ArcToolbox, then of course you won't see the messages.


Hope that helps.
Wayne


I tried that and same thing and pasted that code in (yes, running and editing from toolbox with a script tool). No error or message 😞
0 Kudos