2

The following Python code is what I have already tried in field calculator of ArcMap 10.5.1:

Pre-Logic Script Code:

def ConcatAddr(!JOIN_KEY!)
      val = ""
          if !Address! <> val
              return !Address! + " "
          else
              return val
          if !StreetName! <> val
              return !StreetName! + " "
          else
              return val
          if !Apartment! <> val
              return !Apartment! + " "
          else
              return val
          if !CityCode! <> val
              return !CityCode! + " "
          else
              return val
          if !ZipCode! <> val
              return !ZipCode! + " "
          else
              return val

JOIN_KEY =
ConcatAddr(!JOIN_KEY!)
6
def ConcatAddr(Adress,Streetname,Apartment,Citycode,Zipcode):
    return ' '.join([str(i) for i in [Adress,Streetname,Apartment,Citycode,Zipcode] if i not in(None,' ')])

Use Python parser and call with

JOIN_KEY=

ConcatAddr(!Adress!,!Streetname!,!Apartment!,!Citycode!,!Zipcode!)

(You can also define the function as:

def ConcatAddr(*args):
    return ' '.join([str(i) for i in args if i not in(None,' ')])

and it will take any number of inputs)

0

Rewrite your function to this:

def ConcatAddr(!JOIN_KEY!)
    vals = []
    vals.append(!Address!)
    vals.append(!StreetName!)
    vals.append(!Apartment!)
    vals.append(!CityCode!)
    vals.append(!ZipCode!)
    nvals = [x for x in vals if x <> '' and x <> None]
    return " ".join(nvals)
0
def ConcatAddr(!JOIN_KEY!)
      val = ""
          if !Address! <> val
              return !Address! + " "
          else
              return val
          if !StreetName! <> val
              return !StreetName! + " "
          else
              return val
          if !Apartment! <> val
              return !Apartment! + " "
          else
              return val
          if !CityCode! <> val
              return !CityCode! + " "
          else
              return val
          if !ZipCode! <> val
              return !ZipCode! + " "
          else
              return val

JOIN_KEY =
ConcatAddr(!JOIN_KEY!)

Should be

def concatenate_address(address, street_name, apartment, city_code, zip_code):
"""This function creates a full address by concatenating fields"""

    full_address = ""

    if address:
            full_address = "{}".format(address)

    if street_name:
            full_address = "{} {}".format(full_address, street_name)

    if apartment:
            full_address = "{} {}".format(full_address, apartment)

    if city_code:
            full_address = "{} {}".format(full_address, city_code)

    if zip_code:
            full_address = "{} {}".format(full_address, zip_code)

    return full_address

Then you call it like this

JOIN_KEY = ConcatAddr(!Adress!,!Streetname!,!Apartment!,!Citycode!,!Zipcode!)

You need to keep things simple that way you can actually read and modify this later to build your python knowledge.

The reason this code should work when yours didn't is that you returned after every single if statement. Return statements exit the function in python so your code would only have returned your field plus a blank space if it worked at all. <> is not how you express not equals in python. In python not equals is !=.

I believe you can get away with not having a not equals operator in your function because blank and null values equate to False in python. If statements test for True so if your string isn't blank or null, it should be True and strings should be concatenated.

You can leave your fields till the end when you call the function because the things between the parenthesis in your function definition line, def concatenate_address(address, street_name, apartment, city_code, zip_code):, are called parameters. Python uses these parameters to know what to do with your arguments, which would be what you pass to them when you call the function. When you pass in your actual fields, python replaces the parameters with those fields, your arguments, when it runs your function.

Please leave a comment on my answer if you have any questions and I will get back to you. Also, try to adopt my formatting. It's the proper way to right python and extremely readable. You will thank me in 6 months when you're looking at this again and have no idea what you wrote.

0

Similar (well, equivalent really) to @BERA 's answer, you could use Python's built-in filter function:

' '.join(filter(
    lambda s: (s or '').strip(),
    [!Adress!,!Streetname!,!Apartment!,!Citycode!,!Zipcode!]
 ))

Filter will keep items from the list which are logically True. Empty strings (and None) values are logically False, so the lambda simply returns a stripped string value. Again, you can call this with the Python parser.

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.