1

I'm trying to use field calculator to assign values to a new field I created. I wan't to assign the value from the first field unless that field is empty, if it is empty use field 2, if that is empty use field 3. Here is what I have thus far. I feel it is way off but not sure where to go from here.

New_Size: My new field

wmDeviceEq: First field to check for new field name

wmDevice_4: Second ""

wmDevice_5: Third ""

Field_Calculator_Image

2
  • What happens? Please, update the question with relevant information. Nov 23, 2015 at 17:21
  • Nothing happens, I get an error for syntax on second line.
    – Jeff C
    Nov 23, 2015 at 18:09

2 Answers 2

1

In the codeblock, you can only use parameters, not field names. You'll need to create parameters for each of your field names, like so:

def Reclass(f1, f2, f3):
    New_size = f1 # this doesn't actually do anything
    if f1 == '':
        return f2
    elif f2 == ''
        return f3
    else: # is this cutoff?
        return f1 # New_size maybe?

and then

Reclass(!wmDeviceEq!, !wmDevice_4!, !wmDevice_5!)

What if f3 (wmDevice_5) is empty? Not sure if your picture is showing a cuttoff else statement.

Are the values in the fields actually empty strings? Shapefiles store a single space " " and feature classes in a geodatabase typically use <Null>, which in Python would be None.

If the fields are actually empty strings and only one field will ever have data, you don't need the code-block at all.

"".join((!wmDeviceEq!, !wmDevice_4!, !wmDevice_5!))

As an aside, a nested if statement would be something like this:

if result > 10:
    print("Greater than 10")
    if result < 100:
        print("and less than 100")
    else:
        print("It's big...")
else:
    print("Too small!")
2
  • "If the fields are actually empty strings and only one field will ever have data, you don't need the code-block at all. "".join((!wmDeviceEq!, !wmDevice_4!, !wmDevice_5!)) " I think what you gave me at the beggining will get me to what I was needing. but could you explane this join a little more? Also, if all three of the fields are empty then the new column(New_Size) needs to be left empty as well, or assign some kind of filler such as "none" or "unassigned" maybe.
    – Jeff C
    Nov 23, 2015 at 18:04
  • @JeffC, "".join() concatenates a list of strings with the the delimiter, which in this case, is an empty string. If you wanted a comma, say, it'd be ",".join()
    – Paul
    Nov 23, 2015 at 18:36
0

@Paul

I edited the code and came up with this

def Reclass( ph1, ph2, ph3 ):
  New_Size = ph1
  if ph1 == ' ':
      return ph2
  elif ph2 == ' ':
      return ph3
  elif ph3 == ' ':
      print ("NULL")

and then

Reclass(!wmDeviceEq!, !wmDevice_4!, !wmDevice_5!)

When it runs my new field pulls values from only ph2... enter image description here Also not sure about that final elif. it's meant to fill a space that has no value to pull, with a "Null".

I tried the "".join() function but it places spaces every time it moves to the next field, like so. enter image description here

Won't work unless there is a way to remove those spaces before the values in the New_Size field.

3
  • This should probably be a new question or an edit to the original. You can remove extra spaces by adding .strip() so it'd be something like "".join((!wmDeviceEq!, !wmDevice_4!, !wmDevice_5!)).strip()
    – Paul
    Nov 23, 2015 at 21:11
  • Will that also remove the spaces within the text in each field? I have to have the spaces that exist within "35-HR OCR".
    – Jeff C
    Nov 23, 2015 at 22:26
  • the Python docs are great.
    – Paul
    Nov 23, 2015 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.