[Tutor] (no subject)
Steven D'Aprano
steve at pearwood.info
Thu Jan 13 23:53:30 CET 2011
lmhosie at jacks.sdstate.edu wrote:
> Hello All,
> I am having an issue with lists and am not sure where to go from here any advice appreciated. I colored the areas of concern.
> blue my comments
> purple example of output
> red area area concerned
You may have coloured the text before you sent it, but the colours did
not survive being sent to a mailing list. Everything you have written is
a nice, plain black.
Colouring text is no substitute for actually taking the time to explain:
* what you tried;
* what you expected to happen;
* what happened instead.
It is good practice to try to reduce the code to the smallest amount
that actually demonstrates the same problem. For example, in your code
you have this:
[...]
> # Check out any necessary licenses
> gp.CheckOutExtension("spatial")
> # Load required toolboxes...
> gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
> gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
> gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
> gp.workspace = "D:\model"
This is almost certainly irrelevant to the problem. To us, this is just
noise -- we can't run it, because we don't have the arcgisscripting
module, we don't have any way of testing for a licence, we can't load
the toolboxes.
For us to help you, you have to help us -- don't ask us to debug code we
can't run. (We can sometimes try, if the bug is obvious, but usually it
is just frustrating for everybody involved.) Spending the time to
simplify the problem to the point we can run the code will help us to
help you, and it might even let you solve the problem yourself.
Some further comments:
> # Table and field name inputs
> inTable = "D:\model\Files.mdb\Last"
> inPath="D:\model\Path.lyr"
In *this* particular case, this way of writing file names is safe, but
in general, it risks clashing with Python's "character escapes". String
literals like \t \n \r and many others have special meaning to Python,
and are turned into special characters like tab, newline, etc.
Fortunately, Windows accepts both backslash and forward slash for file
names, so the safest and easiest way to write those files names is:
inTable = "D:/model/Files.mdb/Last"
inPath = "D:/model/Path.lyr"
Further comments:
> paths = gp.SearchCursor(inTable)
> path = paths.Next()
> # Create an empty list
> PathList = []
> while path:
> # If the value is not already in the list, append it
> if path.GetValue(inField) not in PathList:
> PathList.append(path.GetValue(inField))
> path = paths.Next()
[...]
> This is the area that I am having complications with I am getting the correct output at the end of print PRList however because of the loop it overwrites the list each time. Ultimately I need to get a PR list of evens and odds
> but when I put the EVEN Odd Check in the loop it will only calculate the first even number then give an error. Traceback (most recent call last):
> File "C:\Users\lwood\Desktop\MODEL SCRIPTS\E\oddeven.py", line 65, in <module>
> if PR.GetValue(PRField) not in PRList:
> AttributeError: 'int' object has no attribute 'GetValue'
This error does not match the code you have written: the error talks
about PR.GetValue, your code says path.GetValue. What other differences
are there? What is PR? How does it differ from path?
It is a waste of time to ask us to debug code that is not the code that
is failing!
The problem is, you have a line that *may or may not* look like this:
paths = gp.SearchCursor(inTable)
We don't know what paths will equal! It could be anything. But from the
name, it should be some sort of collection of path objects, whatever
they are. Each path object should have a method GetValue, but apparently
(guessing from your error) some of them are plain int objects instead of
path objects.
> There are 233 paths and within each a different number of rows. I need a list to make a feature layer of just odds and of evens per each path.
> It works fine when it is out of the loop but only gives me the last path list.
I don't understand how you can say it works fine, when a moment ago you
said it gives an error.
> I realize that this it is looping over the list each time and that is why the output is for the last path only. I would just like it all in one loop is possible but not necessary.
I don't understand what this means.
> I'm not sure but what I have thought may work is writing the list to a file and appending it each time then reopenning it for the calculations but that seems a little much. Perhaps a Mapping technique or dictionary? Or like I mentioned before it would be fine to do it all in one loop as well.
There shouldn't be any need to write to a file. But I can't suggest an
alternative because I don't understand what you are trying to do.
I'm sorry that I can't be of more assistance, but unfortunately my
crystal ball is broken this week *wink*
--
Steven
More information about the Tutor
mailing list