vendredi 23 octobre 2020

write a function extract information form a dic when some key or sub keys is missing

I am trying to read a few information like this and want to extract information of the key "DATE"

{'DATE': {'YEAR': '1595', 'MONTH': 'October', 'DAY': '27'},
 'TIME': {'HR': '12', 'MIN': '20'},
 'AST': 'latitude',
 'ARC': '0° 0’ 25'}

I want to write a function in python, when

key "DATE" or it's sub key like 'YEAR', MONTH' ,'DAY'

is missing. raise an message said

"the information in the text is not enough"

when all other key is give the result like this

('1595', 'October', 27)

I wrote that and it works for cases with informational but when I have case like

{'DATE': {'YEAR': '1583', 'MONTH': 'April'}, 'TIME': {}, 'ARC': '1° 50’', 'ZOD': 'Taurus', 'AST': 'latitude'

which one subkey is misisng raise a error

def converskyfieled(sample):
    for key in sample:
        if "DATE" in key:
            if (sample[key]['YEAR']!= None) & (sample[key]['MONTH']!=None ) & (sample[key]['DAY']!= None):
                a,b,c= sample[key]['YEAR'] ,sample[key]['MONTH'], sample[key]['DAY']
                return((a),b,int(c))
            else: 
                print("not enough inofrmation form the text")

it should not be hard but still working on that, the aboive function is ok but it suckes when we have case that some key is missing

Aucun commentaire:

Enregistrer un commentaire