I am ArcGIS 10 user and I want to create ArcPy script for simple concatenate row values like this tool Concatenate Row Value that works with ArcGIS 10.
I have three fields in my shapefile id,field1,field2
.
Here the image from my shapefile .dbf
after sort by id.
If you see the image you can see the id not have unique values.
I want to create the layer with unique id values but I don't want to loose some values from field2 (is text). For field2 I need to use for next conditions.
In Excel if I use some condition like this:
=IF(AND(A3=A2;C3=C2);D2;IF(AND(A3=A2;C3<>C2);D2;IF(AND(A3=A2;C3<>C2);D2&","&C3;C3)))
Then I take new field to like this:
That works in Excel, I want to do with Python or ArcPy or some tool from ArcGIS.
In QGIS, it is easy for me to do with this plugin that does dissolve with stats
.
I try to follow this code:
updateFields = ["id", "field2", "field3"]
with arcpy.UpdateCursor("featurelayer", updateFields) as cursor:
for row in cursor:
if row[0]+1 == row[0] and row[2]+1==row[2]:
row[3] = row[2]
elif row[0]+1 == row[0] and row[2]+1!=row[2]:
row[3] = row[2]
elif row[0]+1 == row[0] and row[2]+1!=row[2]:
row[3] = row[2]+','+row[2]+1
cursor.updateRow(row)
That looks correct but any time to try to run it I take this error:
Cursor AttributeError: __exit__
How do I fix the problem?
Best Answer
If I understand your question correctly, you want to concatenate three fields into a new field. You can use the field calculator say you create a new field called "conc" for your concatenated strings.
eg. field1=dog field 2=cat, field3=moose
Right click on the "conc" field and go to field calculator, change to python:
Then write:
To concatenate all three fields into your new conc field. Giving you:
See the ESRI blog for more concatenation.
Based on the new edits:
There's a tool already called "Concatenate Row Values" that looks to do exactly what you want - which takes identical row values (like your id field) and concatenates the values in a given row into a new one.