Select to view content in your preferred language

Concatenation of Fields

312
1
05-08-2023 09:37 PM
Bradmayger
New Contributor II

I need to concatenate 8 or 9 fields, using "/" between the text, and no spaces if it is blank, the following code had worked, but has since failed.

def update(a, b, c, d, e, f, g, h):
strArray = []
if a != ' ':
strArray.append(a + "/")
if b != ' ':
strArray.append(b + "/")
if c != ' ':
strArray.append(c + "/")
if d != ' ':
strArray.append(d + "/")
if e != ' ':
strArray.append(e + "/")
if f != ' ':
strArray.append(f + "/")
if g != ' ':
strArray.append(g + "/")
if h != ' ':
strArray.append(h + "/")

str = "".join(strArray)
str = str.strip()
str = str.lstrip("/")
str = str.rstrip("/")
return str

BFS_concat = update(!GDE_01B!, !GDE_02B!, !GDE_03B!, !GDE_04C!, !GDE_05B!, !GDE_07B!, !GDE_08B!, !GDE_09B!)

 

 

Any ideas please ?

 

Cheers

Brad 

0 Kudos
1 Reply
DanPatterson
MVP Esteemed Contributor

Error message would be useful.

Are there  any nulls in the fields? and are they all text?

a, b, c, d, e, f, g, h
('a', 'b', 'c', 'd', 'e', '', 'g', 'h')

flds = [a, b, c, d, e, f, g, h]

def update(a, b, c, d, e, f, g, h):
    """concatenate flds"""
    out = ""
    for i in flds:
        if i:
            out += i + "/"
    if out[-1] == "/":
        return out[:-1]
    return out.strip()
   
update(a, b, c, d, e, f, g, h)
'a/b/c/d/e/g/h'

  If so, the above example would seem simpler 


... sort of retired...
0 Kudos