if else

738
3
12-07-2017 01:30 PM
JaredPilbeam2
MVP Regular Contributor

ArcMap Version: 10.4.1

I have a script that uses a metadata module to check what the metadata of a feature class is. It's probably something simple, but I can't explain why only two of the four if else statements are working the way I want them to. I'm trying to print the title, tags, summary and description. And when there is no text my script is supposed to tell it to print the else statement. Title and tags seem to work but not summary and description.

As you can see from the PrtScn the feature class has no info except for the title.

import arcpy
import arcpy_metadata as md

metadata = md.MetadataEditor(r'\\gisfile\GISstaff\Jared\Attractions.gdb\RT66')

def meta2txt():
    title = metadata.title
    tags = metadata.tags
    purpose = metadata.purpose
    abstract = metadata.abstract
        
    if title == metadata.title:
        print('Title:\n{}\n'.format(title))
    else:
        print('Title: \nThere is no title.\n')

    if tags == metadata.tags:
        print('Tags:\n{}\n'.format(tags))
    else:
        print("Tags: \nThere are no tags.\n")

    if purpose == metadata.purpose:
        print('Summary:\n{}\n'.format(purpose))
    else:
        print('Summary: \nThere is no summary.\n')

    if abstract == metadata.abstract:
        print('Description:\n{}\n'.format(abstract))
    else:
        print('Description: \nThere is no description.\n')
##
meta2txt()

As you can see the title has been printed and the tags' else condition has printed the string too. The Summary and the Description is supposed to print 'There are no tags.' as Tags has. Using the debugger, it stops on lines 23 and 28 as if it wants to print them, but doesn't. That's what I don't understand.

Tags (1)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

It probably worked but isn't returning what you want

a = ''

if a is not None:
    print('Description:\n{}\n'.format(a))
    
Description:

# ---- hmmmm ... but it actually worked

# ---- better for testing at least... use another property in the output

if a is not None:
    print('Description:\n{}{}\n'.format(a, len(a)))
    
Description:
0
JaredPilbeam2
MVP Regular Contributor

Thanks Dan.

Using if... is not None it printed fine. But, when there is no text and it should jump to the else statement, it doesn't. Using this feature class with the Summary (purpose) missing it never runs the else statement:

import arcpy
import arcpy_metadata as md

metadata = md.MetadataEditor(r'\\gisfile\GISstaff\Jared\Attractions.gdb\Metra')

def meta2txt():
    title = metadata.title
    tags = metadata.tags
    purpose = metadata.purpose
    abstract = metadata.abstract

    if title is not None:
        print('Title:\n{}\n'.format(title))
    else:
        print('Title: \nThere is no title.\n')

    if tags is not None:
        print('Tags:\n{}\n'.format(tags))
    else:
        print("Tags: \nThere are no tags.\n")

    if purpose is not None:
        print('Summary:\n{}\n'.format(purpose))
    else:
        print('Summary: \nThere is no summary.\n')

    if abstract is not None:
        print('Description:\n{}\n'.format(abstract))
    else:
        print('Description: \nThere is no description.\n')

meta2txt()

0 Kudos
DanPatterson_Retired
MVP Emeritus
if tags is not in (None, ''):
    print("good --{}--".format(tags))
else
    print("bad --{}--".format(tags))

perhaps... it is either None if using a geodatabase, or '' and empty string or if there is a space (heaven forbid) then you could add ' ' (one space) and hope that there aren't 2 spaces... The list of possibilities goes on