Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JSON / Dict search and value updates

I have a bunch of data entries that look as follows:

{'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 through this data and set the 'periodStartDate' value to the date one day after the prior period's entry for 'periodEndDate' (of the same formType). I'm comfortable using datetime and the date additions, etc, but I am not sure how to cross-reference the different line items in this file to extrapolate the periodStartDate.

Also, the 'formType':'amend' needs to set its 'fiscalYearEnd' value based on using the year that its wedged between from the above 'formType':'tax' key-value pairs.

like image 445
JackBurton Avatar asked Jul 15 '26 19:07

JackBurton


1 Answers

I would import each line into a list of dicts then iterate over that list to get the result you want using datetime.timedelta

I converted the dicts you provided into a list to simulate 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 be something like:

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
like image 122
Mike - SMT Avatar answered Jul 17 '26 17:07

Mike - SMT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!