1

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.

image1

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:

new field

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?

  • use the calculate filed (data mangement) in arcpy to do this. here is the help docs pro.arcgis.com/en/pro-app/tool-reference/data-management/… – NULL.Dude Jun 27 '17 at 16:45
  • @Joe i have read this many times but that doc not help for my question,how to this with calculate filed ? – Mar Jun 27 '17 at 16:47
  • Can you edit your question to be more clear? What are the conditions of concatenating your fields? Do you simply want all three together (see below)? Or to have if statements like your provided code? If so please type out these conditions or what you have tried, the code provided in incoherent imo. – GISKid Jun 27 '17 at 17:04
  • None of your if conditions will ever be true. This has the same problem as the previous post, in that you are trying to use DA cursor syntax with an old-style cursor. – Vince Jun 27 '17 at 18:36
  • 1
    It's not possible for that code to work. You need to start over with an appropriate flow of control template for regular cursors (as in the documentation), then start working on a way to initialize to None and retain the previous row contents before invoking next. Then you'll have a chance at making code which can be tested. As @Midavalo wrote, There is no way for us to explain how to fix your code unless you explain what you think it should be doing. – Vince Jun 27 '17 at 19:56
4

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:

!field1! + !field2! + !field3!

To concatenate all three fields into your new conc field. Giving you:

dogcatmoose

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.

  • that i need but i want to do this only in features where have some value in my field1 (look the picture in my post) – Mar Jun 27 '17 at 19:28
  • @Mar myself and the other commenters do not understand what your conditions are - I would edit your post to make that clearer and we can try and help. – GISKid Jun 27 '17 at 20:08
  • i have update my ask – Mar Jun 29 '17 at 19:37
  • @Mar based on your edit, there is already a tool that does this:blogs.esri.com/esri/arcgis/2012/07/05/concatenaterowvalues – GISKid Jul 2 '17 at 17:52
  • but that tool not work on ArcGIS 10.0,see the comments – Mar Jul 3 '17 at 2:02
0

You'll want to iterate with a cursor and then make use of a dictionary to store past values found.

#empty dictionary to hold past values
di = {}

#cursor to iterate rows
with arcpy.da.UpdateCursor ("featurelayer", ["id", "field2", "final_field"]) as curs:
    for Id, val, finalVal in curs:
        #update dictionary
        try: di [Id] += [val]
        except: di [Id] = [val]

        #get all values and create string
        vals = di [Id]
        valStr = ",".join (map (str, vals))

        #update row
        row = (Id, val, valStr)
        curs.updateRow (row)

For 10.0 and before:

#empty dictionary to hold past values
di = {}

#cursor to iterate rows
curs = arcpy.UpdateCursor ("featurelayer")
for row in curs:
    #update dictionary
    Id = row.getValue ("id")
    val = row.getValue ("field2")
    try: di [Id] += [val]
    except: di [Id] = [val]

    #get all values and create string
    vals = di [Id]
    valStr = ",".join (map (str, vals))

    #update row
    row.setValue ("final_field", valStr)
    curs.updateRow (row)

del curs
  • can I use .da in ArcGIS 10.0 ? – Mar Jul 3 '17 at 2:00
  • @Mar Negative. See edit. – Emil Brundage Jul 3 '17 at 14:41
  • your code dont work,you are sure ? – Mar Jul 9 '17 at 0:25

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.