2

My programming is really rusty at this point, and I am trying to do a simple calculation using a Codeblock, either VB or Python is fine. I have tried about 50 different variations on same theme, and get a runtime error every time.

Here's a screenshot of my Field Calculator. Basically, Field !Items! contains a string of all items separated by a comma, and I am trying to create new fields that contain a "y" if that item is present in !Items!, in this case it's "Baby Items". It is always capitalized like so, no need to worry about case mismatch.

def calc (items)
  if items.find("Baby Items") != -1:
    return "y"

calc (!Items!)

Field Calc Screencap

  • Please include your code as text rather than a picture so that potential answered can copy/paste it for testing. – PolyGeo May 10 '17 at 20:26
  • You could find all the rows containing 'Baby Items' using items like '%Baby Items%' query and calculate baby = "y".. note the wildcard is different for data types, percent works for shapefiles and file geodatabases but for personal geodatabases use asterisk as the wildcard. see desktop.arcgis.com/en/arcmap/10.3/map/working-with-layers/… and search for LIKE. Note some data types have case sensitive SQL but some do not. – Michael Stimson May 10 '17 at 21:38
3

Almost there. Try this:

def calc(items):
    if "Baby Items" in items:
        return "y"

This will check for the string "Baby Items" contained in the field items.

Also remember the colon : after def calc(items) -> def calc(items):

  • Ahh, thanks, it was the colon after (items), I knew it was something ridiculous like that!!! I started using "in", and then kept re-inventing the wheel from there. – Masha Josefson May 10 '17 at 20:31
  • It won't let me upvote since I am a n00b here! – Masha Josefson May 10 '17 at 20:41

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.