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.
Field1 | Field2 | Feild3 | Field4 |
/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.
Field1 | Field2 | Feild3 | Field4 |
/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)
Solved! Go to Solution.
r = [row[0], row[1], row[2]] # --- being explicit
out = "".join([i for i in r if i]).replace("//", "/")
To take care of potential '//'
r = [row[0], row[1], row[2]] # --- being explicit
out = "".join([i for i in r if i]).replace("//", "/")
To take care of potential '//'
Awesome, that worked. Thank you.
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_