Select to view content in your preferred language

Python Conditional Concatenation

4341
4
07-27-2010 09:04 AM
AndrewHunt
Emerging Contributor
BACKGROUND:
-2 fields I wish to concatenate into one, on the condition that if BOTH fields have a value in them, only one is copied into the new concatenated field (either one, doesn't matter). If only one field has a value, that's the one that's transfered. If neither field has a value, the new concatenated field is blank.
-All fields are strings
- ArcInfo, 9.3.1 SP2

ISSUE:
-Using VBA in the field calculator with IF and ELSE statements, I keep getting errors such as blank objectID, or the such. The other option would be python, for which I have no knowledge.  What would the python statement look like?

Many thanks, I've wasted 4 hours on this so far.
0 Kudos
4 Replies
ChrisSnyder
Honored Contributor
Using an update cursor in Python, it would look something like this:

import arcgisscripting
gp = arcgisscripting.create(9.3)
inputFC = r"C:\temp\test.gdb\test"
fieldName1 = "THIS_FIELD"
fieldName2 = "THAT_FIELD"
concatFieldName = "CONCAT_FIELD"
updateRows = gp.updatecursor(inputFC)
updateRow = updateRows.next()
while updateRow:
   if updateRow.getvalue(fieldName1) == None and updateRow.getvalue(fieldName2) != None:
      updateRow.setvalue(concatFieldName, updateRow.getvalue(fieldName2))
   elif updateRow.getvalue(fieldName1) != None and updateRow.getvalue(fieldName2) == None:
      updateRow.setvalue(concatFieldName, updateRow.getvalue(fieldName1))
   elif updateRow.getvalue(fieldName1) != None and updateRow.getvalue(fieldName2) != None:
      updateRow.setvalue(concatFieldName, str(updateRow.getvalue(fieldName1)) + "-" +  str(updateRow.getvalue(fieldName2)))
   else:
      print "FUBAR"
   updateRows.UpdateRow(updateRow)
   updateRow = updateRows.next()
del updateRow
del updateRows
0 Kudos
ChrisSnyder
Honored Contributor
Using an update cursor in Python, it would look something like this:

import arcgisscripting
gp = arcgisscripting.create(9.3)
inputFC = r"C:\temp\test.gdb\test"
fieldName1 = "THIS_FIELD"
fieldName2 = "THAT_FIELD"
concatFieldName = "CONCAT_FIELD"
updateRows = gp.updatecursor(inputFC)
updateRow = updateRows.next()
while updateRow:
   if updateRow.getvalue(fieldName1) == None and updateRow.getvalue(fieldName2) != None:
      updateRow.setvalue(concatFieldName, updateRow.getvalue(fieldName2))
   elif updateRow.getvalue(fieldName1) != None and updateRow.getvalue(fieldName2) == None:
      updateRow.setvalue(concatFieldName, updateRow.getvalue(fieldName1))
   elif updateRow.getvalue(fieldName1) != None and updateRow.getvalue(fieldName2) != None:
      updateRow.setvalue(concatFieldName, str(updateRow.getvalue(fieldName1)) + "-" +  str(updateRow.getvalue(fieldName2)))
   else:
      print "FUBAR"
   updateRows.UpdateRow(updateRow)
   updateRow = updateRows.next()
del updateRow
del updateRows
0 Kudos
ChrisSnyder
Honored Contributor
True, I guess I didn't read the requirements very well:o

Concatenation: A + B = AB.
Requirements description: If A or B then A or B, else nothing
0 Kudos
RichardFairhurst
MVP Alum
True, I guess I didn't read the requirements very well:o

Concatenation: A + B = AB.
Requirements description: If A or B then A or B, else nothing


Chris:

I am not sure how best to contact you directly, but I have been trying to modify a script you wrote and posted on the public scripts page about a year ago.  Your script permanently sorts a feature class or table to a new output feature class or table.

I have been trying to modify the script to optionally add a field to the output that will store a copy of the original data source's OID field values.  I need this where I want to join the output back to the source, but the data source has no other unique ID than the OID field and I cannot alter the source data schema to ensure that its OID values transfer to the output table created by your script.  Your script by default drops the original OID values from the output.  I have some code that basically works if I can assume that all OID fields are actually named "OBJECTID", but I do not think that is the case.

In VBA there are methods that can query a data source to confirm it has an OID and then get the OID field's name (which tells me there is more than one way these fields can be named).  Is there anything equivalent in Python?  Or how would you approach checking these two aspects of a data source to add appropriate error checking routines to your Python script to handle a subroutine like the one I want?

Thanks for the script and I hope you can help me.

Rich
0 Kudos