I have the following which I can successfully get my desired results with the input prompt standalone script. Yet, I prefer that a tool I created to be used and display the results. What am I missing in order to view the results?
#Import Modules import csv,os,arcpy #Set variables ifile = open('PythonShellSave.csv','r') reader = csv.reader(ifile) featureclass = input("Name of the feature class ? ") #featureclass = arcpy.GetParameterAsText(0) #Set path location of Excel (CSV) file path = 'C:\WriteMxdInfoToExcel' os.chdir(path) #CSV Module terminology row is horizontal & field is vertical;column is horizontal & row is vertical for excel. for row in reader: for field in row: if field == featureclass: print row [4] + " " + featureclass arcpy.SetParameterAsText(1,row) #Close Excel (CSV) file ifile.close()
are you wanting to see the equivalent of a print statement within arcmap?
if so AddMessage—Help | ArcGIS for Desktop
you may find it easier to accumulate messages then just include 2 lines so you can 'print' witnin and out arcmap
.....
msg += "more stuff"
arcpy.AddMessage(msg) # for inside arcmap
print(msg) # python 2 and 3 compliant
Yes equivalent of a print statement within arcmap.
I just want to view results.
I will take a look about AddMessage, thank you.
for example your line
print row [4] + " " + featureclass
could be
msg = row[4] + " " + featureclass
print(msg)
arcpy.AddMessage(msg)
If you want something more generic with a time stamp (sorry about the %formatting instead of the {} .format style...haven't updated it for a while)
import time import arcpy def timeStamp(): """ returns time stamp. """ return time.strftime(' -- %B %d - %H:%M:%S') def myMsgs(message): arcpy.AddMessage(message + ' %s' %(timeStamp())) print(message) # + ' %s' %(timeStamp()))
then if you enter
myMsgs("you message here with whatever formatting {0}".format(variableToFormat))
the myMsgs will work in either without having to add both lines. works for me (in my_utils.py file) but it is a pain when I have to share my code on the forums.
Thank you for the input. However I am not familiar enough with the time module nor what the % means. I am not aware of .format either. As you can see I am beginner and I am just learning and attempting to properly use the Add Message, which I am still working on.
Sorry about that Devin, I did a quick copy paste. Here is a simpler version with the time module removed. I just have that in there so I could get a time stamp on each message so I knew how long each process was taking...not a necessary component at all. This sample imports arcpy, creates the simple myMsgs function and has five samples using it with the function. This should work in the python window, or if you run it in a arcpy script (.py file).
Sample 1: just text
Sample 2: a variable
Sample 3: some text, two variables and a blank line after The {#} line up with the variables in the .format( ) part. The # isn't mandatory, but good practice and allow you to reorder or even repeat the use of a variable in the list
Sample 4: some text and some math with the number variable
Sample 5: reusing one of the variables in the list.
These formats work with both the print and AddMessage
import arcpy def myMsgs(message): arcpy.AddMessage(message) print(message) somethingToPrint = "print this message" aNumber = 5 myMsgs("printing just a line of text I type here") myMsgs(somethingToPrint) myMsgs("printing {0} with a number {1} with a blank line after \n".format(somethingToPrint, aNumber)) myMsgs("printing with a number + 1: {0} ".format(aNumber + 1)) myMsgs("{1}: printing {0} with a number {1} with a blank line after \n".format(somethingToPrint, aNumber))
Hope this helps
edit: btw, you should check out and bookmark Dan Patterson 's Py... blog for many hints and other links.
Thank you for taking the time to further explain. Will this work in a script that I am using with a tool I created? To automatically display message to results? I tested out the function but it is not automatic when run and seen in python shell. However, as mentioned, my actual goal is to use it as a tool.
I will take a look at the Blog, thanks.
yes, it should. The function will know if it is in a script or in the window and print as needed, while ignoring the other. I use this all the time since I switch between the two and it's a hassle to change and/or have both.
Tip: if you are tying to debug but don't know where it is crashing, you can put lots of these within your code maybe even right after each process... I many times do simple numbered statements just to see how far it gets so I can track down an issue..
...a process
myMsgs("here1)
..another process
myMsgs("here2)
Of course you would want something more meaningful for your final script,
I am trying to have Add message for the variable, but I am having trouble calling the function with the variable.
#Import Modules import csv,os,arcpy,time #Set variables ifile = open('PythonShellSave.csv','r') reader = csv.reader(ifile) featureclass = input("Name of the feature class ? ") #featureclass = arcpy.GetParameterAsText(0) #Set path location of Excel (CSV) file path = 'C:\Users\WriteMxdInfoToExcel' os.chdir(path) # Create function def myMsgs(message): arcpy.AddMessage(message) #CSV Module terminology row is horizontal & field is vertical;column is horizontal & row is vertical for excel for row in reader: for field in row: if field == featureclass: final = row[4] + " " + featureclass print final myMsgs (final) #Close Excel (CSV) file ifile.close()