2

I have a folder with an output (shapefiles) from an iteration. On this output, I want to perform a join between two attributetables.

So far so good. Here comes the tricky part. There are different shapefiles (lets say 20), all with different names. Each attributetable has exactly the same amount of fields (maybe the numbers of rows differs). What I want the script to do is: pick two specific attributetables which belong together (based on the name), join them and then go on to the next two attributetables until there are no more tables to join. All shapefiles have different names, but where the %Name% stands in the script, the name is the same.

For example:

abc_clip_diss JOIN abc_full_diss
def_clip_diss JOIN def_full_diss 
ghi_clip_diss JOIN ghi_full_diss

What I got so far and is not much because I am very new to python is that:

import arcpy

#set workspace
arcpy.env.workspace =  

#set variables
inFeatures = "%Name%_clip_diss"
joinField = "WLType"
joinTable = "%Name%_clip_full"
fieldList = ["area_ha"]




#JoinField "JoinField_management (in_data, in_field, join_table, join_field, {fields})"
JoinField_management ("inFeatures" , "joinField" , "joinTable" , "fieldList")

I think a loop would be good here? But I did not understand that good how a loop works. I dont understand what that for XX in YY means. And I am not sure if I can do that thing: %Name%.

Any ideas, hints, suggestions?

EDIT 7/8/14 4:00 pm

import arcpy
import os

#set workspace
arcpy.env.workspace = "C:\\Users\\Documents\\Wetland\\New_Folder"
ws = arcpy.env.workspace

#create a list of features
fcl = arcpy.ListFeatureClasses()

#loop through the list
for fc in fcl:
    basename = fcl.split("_")[0] #split name of features and only take the first word as a basename


    inFeatures = os.path.join(ws, basename + "_clip_diss") #The table or feature class to which the join table will be joined.
    joinField = "WLType" #The field in the input table on which the join will be based.
    joinTable = os.path.join(ws, basename + "_clip_full") #The table to be joined to the input table.
    fieldList = "WLType" #The field in the join table that contains the values on which the join will be based.

    #JoinField "JoinField_management (in_data, in_field, join_table, join_field, {fields})"
    arcpy.JoinField_management(inFeatures , joinField , joinTable , fieldList)

Running this code I run into several problems. The biggest was the Error 000732 which said 'files do not exist'. I converted all my shapefiles into a geodatabase which did the trick. The Geodatabase is in the folder "New_Folder".

But running the code now, still does not work. It does not Join one table to another. I am wondering, where the problem is. Can anyone see it?

3

The attached (untested) script takes the name of a feature class, splits it by "_" and uses that as a basename for the join operations. The general idea is to use:

basename = fc.split("_")[0]

which converts, for example, abc_clip_diss to abc

Then, you can use that basename to create new variables with os.path.join():

inFeatures = os.path.join(ws, basename + "_clip_diss")
joinTable = os.path.join(ws, basename + "_clip_full")

Finally, combine this syntax in a for loop so that you can iterate over a series of feature classes.


import arcpy, os

#set workspace
arcpy.env.workspace = r'C:\path\to\your\fcs'
ws = arcpy.env.workspace

fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    basename = fc.split("_")[0]

    #set variables
    inFeatures = os.path.join(ws, basename + "_clip_diss")
    joinField = "WLType"
    joinTable = os.path.join(ws, basename + "_clip_full")
    fieldList = ["area_ha"]

    #JoinField "JoinField_management (in_data, in_field, join_table, join_field, {fields})"
    arcpy.JoinField_management (inFeatures , joinField , joinTable , fieldList)
  • Thanks a lot. Ill try it in a sec. I have a couple of question to understand everything since my aim is not to get a "ready to use solution" but to learn something for further problems. Loops: It says for fc in fcs: while fc is not defined. How does the loop know how often it should iternate? fc.split("_")[0]: the zero means it starts with the first member of the created list, right? os.path.join(ws, basename + "_clip_diss") Why do we have to write os.path.join and ws again? – Stophface Jul 4 '14 at 19:55
  • and with your edit you just answered all my questions. Ill try to incorporate it! – Stophface Jul 4 '14 at 20:04
  • for loops always work by iterating over a list. To generate that list, we use fcs = arcpy.ListFeatureClasses(), which generates a list of feature classes. Keep in mind the list only contains the name of each FC rather than the full path. fc is defined as a variable in each iteration, so you can call it within the loop. The for loop will iterate over the entire list. If you have 300 FC's in a workspace, then the for loop will iterate over all 300 FCs. ` – Aaron Jul 4 '14 at 20:05
  • It seems like the list fcs is empty. I get an errormessage none type object is not iterable in the line with for fc in fcs: – Stophface Jul 8 '14 at 13:37
  • @Stophface Thanks for pointing out the error. I have fixed the script. – Aaron Jul 8 '14 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.