2

I'm trying to create a simple join script in python from an arcgis model, where the only steps are joining a map to a table to produce a map with the table's properties. The attributes joined together are "CNTYFPID" from the shapefile map and "fips" from the textfile. I have tried editing the script produced by arcgis, but it's not working. Where am I going wrong?

# Import arcpy module
import arcpy
arcpy.env.workspace = "C:/Users/Documents/ArcGIS/Testing123.mdb"
arcpy.env.overwriteOutput = True

# Script arguments
Map = arcpy.GetParameterAsText(0)
if Map == 'tl_2009_us_county_albers_USGS.shp' or not Map:
    Map = "C:/Users/Documents/ArcGIS/Python/tl_2009_us_county_albers_USGS.shp" #provide a default value if unspecified

Crosswalk = arcpy.GetParameterAsText(1)
if Crosswalk == '#' or not Crosswalk:
    Crosswalk = "C:\\Users\\Documents\\ArcGIS\\Testing123.mdb\\cao_fips_crosswalk_for_gis" # provide a default value if unspecified)

# Local variables:
Map = "C:\\Users\\Documents\\ArcGIS\\geography\\tl_2009_us_county\\tl_2009_us_county_albers_USGS.shp"
Final = Map

arcpy.MakeTableView_management(Crosswalk, "fips")

#Process: Join Field
arcpy.AddMessage("Performing Join Field")
arcpy.JoinField_management(Map, "CNTYIDFP", Crosswalk, "fips", "")

The python version I am using is 2.7 and the arcgis version is 10.2. This is what it looks like in modelbuilder

Error message: "Traceback (most recent call last): File "C:/Users/Documents/ArcGIS/Yo2.py", line 28, in arcpy.MakeTableView_management(Crosswalk, "fips") File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6306, in MakeTableView raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset C:\Users\Documents\ArcGIS\Testing123.mdb\cao_fips_crosswalk_for_gis does not exist or is not supported Failed to execute (MakeTableView)."

  • Are both the joining fields the same data type (e.g. number to number or text to text)? – artwork21 Jun 20 '14 at 19:53
  • It may be helpful if you add why it's not working (error messages, etc). – Evil Genius Jun 20 '14 at 19:53
  • Have you tried just running the join script without the if statements? The if statements look odd to me. – GISKid Jun 20 '14 at 19:53
  • Yes, the model itself works in modelbuilder, I just can't manage to figure out how to convert it to python. I'll add the error message to the original post here – Taylor Jun 20 '14 at 19:57
  • 1
    Try removing the Make Table View statement, the standalone script example here does not use that method, help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… – artwork21 Jun 20 '14 at 20:16
3

I've not used JoinField_management. Every time I want to join I've used AddJoin_management. They appear to do very similar things, so it is possible that the Add Join would work for you.

I am thinking that the problem with your script is that the Join data is of type feature class, not that Python has strong types, but both tools specifically call for layer or table view. The good news is that it's easy to do this.. use MakeFeatureLayer_Management to create a layer.

arcpy.AddMessage("Performing Join Field")
arcpy.MakeFeatureLayer_Management(Map,"Map_Layer")
arcpy.JoinField_management("Map_Layer", "CNTYIDFP", Crosswalk, "fips", "")

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.