Select to view content in your preferred language

Joining a shapefiles attributes to a table in ArcGIS 10 using Python

4694
5
03-13-2014 11:37 AM
MoniqueDawson
New Contributor
Hi Experts,

I am trying to join a shapefiles attributes to a table in ArcGIS 10 using Python.
This is the code I have been using. What do the errors mean?? Why are error occuring?

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "R:\Dawson, Monique\Canada_Weather_Grid"

Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 2)
>>> arcpy.JoinField_management("nodes_gp_100.csv", "ID", "Alberta_Weather_Nodes", "ID")
...
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000339: Input nodes_gp_100.csv does not have OIDs Failed to execute (JoinField).
>>> arcpy.JoinField_management("Alberta_Weather_Nodes", "ID", "nodes_gp_100.csv", "ID")
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 999999: Error executing function. Failed to execute (JoinField).
>>> arcpy.JoinField_management("GP_100", "ID", "Alberta_Weather_Nodes", "ID")
Tags (2)
0 Kudos
5 Replies
JoshuaChisholm
Regular Contributor
Hello Monique,

I'd start by changing your env.workspace line. When specifying a path in python you have to do one of the three actions:

  1. Replace all \'s with /'s

  2. env.workspace = "R:/Dawson, Monique/Canada_Weather_Grid"

  3. Replace all \'s with double \'s

  4. env.workspace = "R:\\Dawson, Monique\\Canada_Weather_Grid"

  5. (My personal favorite)Add an 'r' in front of the string

  6. env.workspace = r"R:\Dawson, Monique\Canada_Weather_Grid"

This is because python sees "\" as a special character.

Let me know if this fixes your problem or not. Good luck!
0 Kudos
KinleyWinchester
New Contributor III

Struggle with this almost the entire day... just found this answer and option #3 worked like a charm.  Thanks!  Lives will be saved on my ride home today!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Filenames and file paths in Python

Read it when you get home... your safe journey has other stumbling blocks...

0 Kudos
NickJones
New Contributor II
have you tried exporting your CSV to a DBF first?  Also, as a side note, be careful with this join field tool.  They may have fixed this in 10.2, but sometimes the results aren't good when you use a field that isn't numeric to perform the join with this tool.
0 Kudos
BruceHarold
Esri Regular Contributor
0 Kudos