Defining features in a list

Discussion in 'Python' started by M Whitman, Sep 7, 2012.

  1. M Whitman

    M Whitman Guest

    Good Morning,

    I have been recently trying to define all of the features in a list but have been running into errors. I would like to define the features similar tothe following print statement. Any advice would be appreciated. I'm trying to transition my output from a text file to excel and if I can loop through my lists and define them that transition will be cleaner.

    Many Thanks,

    -Matt

    #Author: MGW
    #2012
    import os, datetime, sys, arcpy, xlrd
    from arcpy import env
    submission = "Rev.mdb"
    env.workspace = "C:/temp/"+submission+"/Water"

    #Get Submission totals
    fclist = sorted(arcpy.ListFeatureClasses("*"))
    for fc in fclist:
    print fc+"="+str(arcpy.GetCount_management(fc).getOutput(0))

    print "Complete"
    raw_input("Press ENTER to close this window")

    Output Generated
    WATER_Net_Junctions=312
    WS_Hyd=484
    WS_Mains=2752
    WS_Node=4722
    WS_Vlvs=1078
    WS_WatLats=3661
    WS_WatMtrs=3662
    WTRPLANTS_points=0
    WTRPUMPSTA_points=0
    WTRTANKS=0
    WTR_ARV=10
    WTR_MISC=0
    Complete
    Press ENTER to close this window

    #Get Submission totals
    fclist = sorted(arcpy.ListFeatureClasses("*"))
    for fc in fclist:
    fc=str(arcpy.GetCount_management(fc).getOutput(0))
    #TEST
    print WS_Hyd


    print "Complete"
    raw_input("Press ENTER to close this window")

    Output Generated
    Traceback (most recent call last):
    File "C:\Documents and Settings\mattheww\Desktop\Copy of QAQCexce_2.py", line 14, in <module>
    print WS_Hyd
    NameError: name 'WS_Hyd' is not defined
    M Whitman, Sep 7, 2012
    #1
    1. Advertising

  2. M Whitman

    Dave Angel Guest

    On 09/07/2012 09:42 AM, M Whitman wrote:
    > Good Morning,
    >
    > I have been recently trying to define all of the features in a list but have been running into errors.


    How proficient are you in Python? Could you possibly use terms which
    make sense to someone who doesn't know this arcGIS program? I'm just
    making a wild guess that that's what you're importing with the arcpy
    import. When I do an internet search on arcpy, I see lots of tutorials,
    training, etc. I didn't find a mailing list, but there probably is one.

    The term that needs translating is "feature.'

    > I would like to define the features similar to the following print statement. Any advice would be appreciated. I'm trying to transition my output from a text file to excel and if I can loop through my lists and define them that transition will be cleaner.
    >
    > Many Thanks,
    >
    > -Matt
    >
    > #Author: MGW
    > #2012
    > import os, datetime, sys, arcpy, xlrd
    > from arcpy import env
    > submission = "Rev.mdb"
    > env.workspace = "C:/temp/"+submission+"/Water"
    >
    > #Get Submission totals
    > fclist = sorted(arcpy.ListFeatureClasses("*"))
    > for fc in fclist:
    > print fc+"="+str(arcpy.GetCount_management(fc).getOutput(0))
    >
    > print "Complete"
    > raw_input("Press ENTER to close this window")
    >
    > Output Generated
    > WATER_Net_Junctions=312
    > WS_Hyd=484
    > WS_Mains=2752
    > WS_Node=4722
    > WS_Vlvs=1078
    > WS_WatLats=3661
    > WS_WatMtrs=3662
    > WTRPLANTS_points=0
    > WTRPUMPSTA_points=0
    > WTRTANKS=0
    > WTR_ARV=10
    > WTR_MISC=0
    > Complete
    > Press ENTER to close this window
    >
    > #Get Submission totals
    > fclist = sorted(arcpy.ListFeatureClasses("*"))
    > for fc in fclist:
    > fc=str(arcpy.GetCount_management(fc).getOutput(0))
    > #TEST
    > print WS_Hyd
    >

    There's no variable WS_Hyd, so what did you expect it to do? Do you
    want to be able to fetch the values by name that were printed above? if
    so, I'd suggest a dict, not a list. Lists don't have names for each
    element, just indices.


    >
    >
    > print "Complete"
    > raw_input("Press ENTER to close this window")
    >
    > Output Generated
    > Traceback (most recent call last):
    > File "C:\Documents and Settings\mattheww\Desktop\Copy of QAQCexce_2.py", line 14, in <module>
    > print WS_Hyd
    > NameError: name 'WS_Hyd' is not defined


    As a very rough start, perhaps you could try something like this.
    Remember i don't have the docs, so the only clue I've got is the stuff
    you printed from the first loop.

    table = {}

    fclist = sorted(arcpy.ListFeatureClasses("*"))
    for fc in fclist:
    table[fc] = +str(arcpy.GetCount_management(fc).getOutput(0))

    Now, if you want to print the value for WS_Hyd, it should be available as

    print "value = ", table["WS_Hyd"]

    If you're sufficiently advanced, i could suggest a class-based solution
    where you'd access items by
    mytable.WS_Hyd

    But if you're not yet familiar with class definition and attributes and
    such, we'd better not go there.

    --

    DaveA
    Dave Angel, Sep 7, 2012
    #2
    1. Advertising

  3. M Whitman

    M Whitman Guest

    Dave- By features I was refering to items in the list. For background the arcpy module is used for geoprocessing of geographic information. I'm using my script to get totals for features in a dataset that I receive on a regular basis- for example total number of hydrants, total number of hydrants with null or missing attributes, and total number of hydrants with outlining attributes.

    I am experienced particularly with the arcpy module and I am trying deligently to become more experienced with Python in general. My goal is to fetchvalues by name and then print output by name. print WS_Hyd and then see "484". I have some experience with class definition but a dictionary might be the way to go. I was understanding the response of the Arcpy module buthadn't understood why the list wasn't being defined in my previous loop statement. I appreciate the response. I will look into dict if you have a class definition suggestion I will run with that. Thanks
    M Whitman, Sep 7, 2012
    #3
  4. M Whitman wrote:
    > Good Morning,
    >
    > I have been recently trying to define all of the features in a list but have been running into errors. I would like to define the features similar to the following print statement. Any advice would be appreciated. I'm trying to transition my output from a text file to excel and if I can loop through my lists and define them that transition will be cleaner.
    >
    > Many Thanks,
    >
    > -Matt
    >
    > #Author: MGW
    > #2012
    > import os, datetime, sys, arcpy, xlrd
    > from arcpy import env
    > submission = "Rev.mdb"
    > env.workspace = "C:/temp/"+submission+"/Water"
    >
    > #Get Submission totals
    > fclist = sorted(arcpy.ListFeatureClasses("*"))
    > for fc in fclist:
    > print fc+"="+str(arcpy.GetCount_management(fc).getOutput(0))
    >
    > print "Complete"
    > raw_input("Press ENTER to close this window")
    >
    > Output Generated
    > WATER_Net_Junctions=312
    > WS_Hyd=484
    > WS_Mains=2752
    > WS_Node=4722
    > WS_Vlvs=1078
    > WS_WatLats=3661
    > WS_WatMtrs=3662
    > WTRPLANTS_points=0
    > WTRPUMPSTA_points=0
    > WTRTANKS=0
    > WTR_ARV=10
    > WTR_MISC=0
    > Complete
    > Press ENTER to close this window
    >
    > #Get Submission totals
    > fclist = sorted(arcpy.ListFeatureClasses("*"))
    > for fc in fclist:
    > fc=str(arcpy.GetCount_management(fc).getOutput(0))
    > #TEST
    > print WS_Hyd
    >
    >
    > print "Complete"
    > raw_input("Press ENTER to close this window")
    >
    > Output Generated
    > Traceback (most recent call last):
    > File "C:\Documents and Settings\mattheww\Desktop\Copy of QAQCexce_2.py", line 14, in <module>
    > print WS_Hyd
    > NameError: name 'WS_Hyd' is not defined
    >

    I'm not sure I've understood everything, is this something you're
    searching for:

    fcDict = dict([(str(fc),
    str(arcpy.GetCount_management(fc).getOutput(0))) ) for fc in
    sorted(arcpy.ListFeatureClasses("*")) ])

    print fcDict
    print fcDict['WS_Hyd']

    This is difficult to read because of the online statement, but it does
    basically the following pseudo code:

    fcDict = dict([(feature.name, feature.value) for feature in featureList ])

    Cheers,

    JM
    Jean-Michel Pichavant, Sep 7, 2012
    #4
  5. M Whitman

    Dave Angel Guest

    On 09/07/2012 11:21 AM, M Whitman wrote:
    > Dave- By features I was refering to items in the list. For background the arcpy module is used for geoprocessing of geographic information. I'm using my script to get totals for features in a dataset that I receive on a regular basis- for example total number of hydrants, total number of hydrants with null or missing attributes, and total number of hydrants with outlining attributes.
    >
    > I am experienced particularly with the arcpy module and I am trying deligently to become more experienced with Python in general. My goal is to fetch values by name and then print output by name. print WS_Hyd and then see "484". I have some experience with class definition but a dictionary might be the way to go. I was understanding the response of the Arcpy module but hadn't understood why the list wasn't being defined in my previous loop statement.


    There is a list fclist being defined, just before the loop. But that
    list contains the names of the "features" not the values. So if you
    wanted, you could make a second list containing the values, or you could
    even make a list containing tuples with name & value. But assuming
    there's no particular ordering you care about, that's what a dictionary
    is good at.

    In either case that loop is not creating extra variables with names like
    WS_Hyd. Creating variables with arbitrary names from data can only be
    done with code that's dangerous and prone to injection attacks. You can
    avoid the problem by putting them in some namespace, either a
    dictionary, or a namedtuple, or a custom class.

    > I appreciate the response. I will look into dict if you have a class definition suggestion I will run with that. Thanks


    Anyway, once you have the dictionary, you can indeed work on the values
    in it, in various ways.

    table = {}

    fclist = sorted(arcpy.ListFeatureClasses("*"))
    for fc in fclist:
    table[fc] = +str(arcpy.GetCount_management(fc).getOutput(0))



    Now you can use your captured data to do further processing. Simplest example printing. Suppose order doesn't matter:

    for key in table.iterkeys():
    print key, "=", table[key]

    You could also do this as:

    for key, value in table.iteritems():
    print key, "=", value

    If you want them in the original order, you can use your fclist, which is a list of keys:

    for key in fclist:
    print key, "=", table[key]

    And of course if you want any particular one, you can do

    print table["WS_hyd"]

    Note that in the last case, if you typed the literal key wrong, or if the arcpy removed or renamed one of the keys, you'd get an exception there. To avoid that, you might do something like:

    key = "WS_hyd"
    if key in table:
    print table[key]


    --

    DaveA
    Dave Angel, Sep 7, 2012
    #5
  6. On Fri, 7 Sep 2012 06:42:20 -0700 (PDT), M Whitman <>
    declaimed the following in gmane.comp.python.general:


    > for fc in fclist:
    > fc=str(arcpy.GetCount_management(fc).getOutput(0))


    This does not do what you think it does.

    fc first is bound to the first element of the list fclist

    Then, fc is bound to the string representation of whatever the stuff
    on the right side evaluates into..

    I suspect you need to study the library reference manual on
    "dictionaries"...
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Sep 7, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    409
    Peter Koch Larsen
    Jul 2, 2004
  2. Steven Bethard
    Replies:
    38
    Views:
    677
  3. Jonathan Mcdougall
    Replies:
    2
    Views:
    492
    Kaz Kylheku
    Nov 3, 2005
  4. BillJosephson
    Replies:
    148
    Views:
    2,634
    peter koch
    Jan 27, 2007
  5. Replies:
    2
    Views:
    85
    gabriele renzi
    Jan 11, 2005
Loading...

Share This Page