+ Reply to Thread
Results 1 to 5 of 5

Thread: Loop through list for multivalue input

  1. #1
    Robert Ehrman
    Join Date
    Apr 2010
    Posts
    11
    Points
    12
    Answers Provided
    1


    0

    Default Loop through list for multivalue input

    I have a folder with hundreds of shapefiles each representing a diff. state, each file starts with the prefix for that specific state e.g., al001r.shp, al003r.shp, fl001.shp, fl003.shp...etc. What I'm trying to do is Append the shapefiles for each state into one shapefile via python, in the end having a single shapefile for each state. I'm thinking I need to create a list of each of the prefixes and enumerate through the list thus creating a list of inputs for a multivalue input for the append? This is about as far as I've gotten, can't figure out how to select just the first iteration and push to a list for multi-input?

    Code:
    dir = r"C:\temp"
    gp.workspace = dir
    print(dir)
    files = os.listdir(dir)
    alist = ['al', 'ct', 'dc', 'de', 'fl']
    for file in files:
        sfile = file[0:2]
        if sfile in alist:
            alist[alist.index(sfile)] 
            print(sfile)

  2. #2
    Jake Skinner

    Join Date
    May 2010
    Posts
    1,608
    Points
    1131
    Answers Provided
    180


    0

    Default Re: Loop through list for multivalue input

    Below is an example on how to do this. The code adds all the shapefiles first two characters to a list, then removes the duplicates. It will then merge the related shapefiles together to one shapefile.

    Code:
    lstFCs = arcpy.ListFeatureClasses("*")
    
    list1 = []
    
    # Append first two characters of each shapefile to list
    for fc in lstFCs:
        list1.append(fc[0:2])
    
    # Remove duplicates from list
    list1 = dict.fromkeys(list1)
    list1 = list1.keys()
    
    # Merge feature classes
    for n in list1:
        m = str(n) + "*"
        lstFCs = arcpy.ListFeatureClasses(m)
        arcpy.Merge_management(lstFCs, n + "_merge")
    Last edited by JSkinn3; 07-07-2011 at 04:10 AM.

  3. #3
    Robert Ehrman
    Join Date
    Apr 2010
    Posts
    11
    Points
    12
    Answers Provided
    1


    0

    Default C++ Runtime Error

    Thanks for the suggestion, the code below ended up working....kind of. There's over 800 files and the code works like a dream until it gets to the 417th file, then I get a C++ Runtime Error! for Program: pythonw.exe????

    Code:
     Create the Geoprocessor object
    gp = arcgisscripting.create(9.3)
    dir = r"C:\temp"
    output = r"C:\temp\shp"
    gp.workspace = dir
    
    fcs = gp.ListFeatureClasses("*")
    
    x = '"%s"' % ';'.join(fcs)
    print x
    gp.Append(x, output, "NO_TEST")
    print gp.getmessages()

  4. #4
    Jake Skinner

    Join Date
    May 2010
    Posts
    1,608
    Points
    1131
    Answers Provided
    180


    0

    Default Re: Loop through list for multivalue input

    Try removing the 417th file and then re-execute the script to see if it works. The file may be corrupted.

  5. #5
    Robert Ehrman
    Join Date
    Apr 2010
    Posts
    11
    Points
    12
    Answers Provided
    1


    0

    Default memory leak in loop

    I tried that, it's not the problem. I can watch the memory build up, once it gets to a certain threshhold it bombs out????

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts