Python JSON / Dict search and update updates

I have a set of data records that looks like this:

{'formType': 'tax', 'periodEndDate': '2016-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2016, 'label': 'FY2016'}
{'formType': 'tax', 'periodEndDate': '2015-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2015, 'label': 'FY2015'}
{'formType': 'tax', 'periodEndDate': '2014-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2014, 'label': 'FY2014'}
{'formType': 'amend', 'periodEndDate': '2015-06-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'}
{'formType': 'amend', 'periodEndDate': '2015-09-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'}
{'formType': 'amend', 'periodEndDate': '2014-06-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'}
{'formType': 'amend', 'periodEndDate': '2014-09-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'}

      

I want to search this data and set the periodStartDate value to the date one day after writing the previous period for periodEndDate (from the same form type). I'm comfortable with adding date and time, etc., but I'm not sure how to cross-reference different positions in this file to extrapolate periodStartDate.

In addition, "formType": "modify" needs to be set to "financialYearEnd" based on the use of the year in which it wedged between the specified "formType": "tax" key-value pairs.

+3


source to share


1 answer


I would import each line in list of dicts

, then iterate over that list to get the result you want to usedatetime.timedelta

I converted the dicts you provided to a list to mimic my example.

import datetime

list_of_dicts =[
                {'formType': 'tax', 'periodEndDate': '2016-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2016, 'label': 'FY2016'},
                {'formType': 'tax', 'periodEndDate': '2015-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2015, 'label': 'FY2015'},
                {'formType': 'tax', 'periodEndDate': '2014-03-31', 'periodStartDate': 'NULL', 'fiscalYearEnd': 2014, 'label': 'FY2014'},
                {'formType': 'amend', 'periodEndDate': '2015-06-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'},
                {'formType': 'amend', 'periodEndDate': '2015-09-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'},
                {'formType': 'amend', 'periodEndDate': '2014-06-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'},
                {'formType': 'amend', 'periodEndDate': '2014-09-30', 'periodStartDate': 'NULL', 'fiscalYearEnd': 'NULL', 'label': 'NULL'}
                ]

def searchForYear(year):
    for subdict in list_of_dicts:
        if str(year) == 'NULL':
            print ("Year is NULL")
        elif str(year) == str(subdict['fiscalYearEnd']):
            if input('Do you want update this row? ') == 'yes':
                EndDate = datetime.datetime.strptime((subdict['periodEndDate']),"%Y-%m-%d").date()
                StartDate = EndDate + datetime.timedelta(days=1)
                subdict['periodStartDate'] = StartDate
                print ("StartDate is now ",subdict['periodStartDate'])
            else:
                print ("Did not update")

        else:
            print ("Year does not appear in this row")


submitYear = input("Check and update Year: ")
searchForYear(submitYear)

      



The result should look something like this:

Check and update Year: 2016
Do you want update this row? yes
StartDate is now  2016-04-01
Year does not appear in this row
Year does not appear in this row
Year does not appear in this row
Year does not appear in this row
Year does not appear in this row
Year does not appear in this row

      

+3


source







All Articles