Concentrate multiple fields, some have blanks.

374
3
Jump to solution
11-04-2022 10:29 AM
2Quiker
Occasional Contributor II

I have a feature class that I need to concentrate multiple fields into one, but the issue is that some have blanks or nulls.

I need Field1, Field2, Field3 concentrated into Field4.

The feature class tables looks like this after I run my code below.

Field1Field2Feild3Field4
/Blue/ /yellow//Blue/none/yellow/
 /Red//Yellow/none/Red/Yellow
/Purple//Pink/ /Purple/Pink/none

 

I need this. If there is a blank or null I don't need it in Field4.

Field1Field2Feild3Field4
/Blue/ /yellow//Blue/yellow/
 /Red//Yellow//Red/Yellow/
/Purple//Pink/ /Purple/Pink/

 

I have the following code.

PTa = "feature class"
with arcpy.da.UpdateCursor(PTa, ['Field1','Field2','Field3','Field4']) as cursor:
    for row in cursor:
        row[3] = '{} {} {}'.format(row[0],row[1],row[2])
        cursor.updateRow(row)

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

 

r = [row[0], row[1], row[2]]  # --- being explicit
out = "".join([i for i in r if i]).replace("//", "/")

To take care of potential '//' 

 


... sort of retired...

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

 

r = [row[0], row[1], row[2]]  # --- being explicit
out = "".join([i for i in r if i]).replace("//", "/")

To take care of potential '//' 

 


... sort of retired...
2Quiker
Occasional Contributor II

Awesome, that worked. Thank you.

0 Kudos
RhettZufelt
MVP Frequent Contributor

This will concatenate them and ignore nulls.  Might need to add a .replace as @DanPatterson suggested if '//' are an issue.

row[3] = ''.join(filter(None, [row[0], row[1], row[2]]))

R_