2

I am reading from an excel, and need to create a json file from it. Iterating through the rows, i am trying to create a dictionary per row and append it to a list, and then that list will be the value of one particular json key.

using:

dates = []
d_tems = {}

for k,v in data.iterrows():
    if (v["recommended_planting_date"] != "NA"):
        d_tems.update({"start_date":str(v["recommended_date"]).strip()})
        d_tems.update({"date_range":str(int(v["date_range"]))}) 
        print(d_tems)
        dates.append(d_tems)

it seems to update even the values already appended to the list, giving me

[{'start_date': '01/08', 'date_range': '60'},
 {'start_date': '01/08', 'date_range': '60'}]

instead of

[{'start_date': '01/03', 'date_range': '25'},
{'start_date': '01/08', 'date_range': '60'}]

i am trying to eventually get to:

"dates": [
        {
          "start_date": '01/03',
          "date_range": 25
        },
        {
          "start_date": 01/08,
          "date_range": 60
        }]

could i get some guidance on this? the number of dictionaries to be created is not known before hand.

Alvin Wanda
  • 179
  • 2
  • 9

2 Answers2

2

You declared d_tems outside of the loop, so you're using the same instance on each iteration,

  • when updating d_tems: there is only one dict so it erases the previous values
  • when appending to dates : you add the same so you see it multiples times in the list

Create it inside the loop

dates = []

for k,v in data.iterrows():
    d_tems = {}
    if (v["recommended_planting_date"] != "NA"):
        d_tems.update({"start_date":str(v["recommended_date"]).strip()})
        d_tems.update({"date_range":str(int(v["date_range"]))}) 
        print(d_tems)
        dates.append(d_tems)

You can simplify your code by removing the extra variable and create+append at the same time, depending the level of code you want

dates = []    
for k,v in data.iterrows():
    if (v["recommended_planting_date"] != "NA"):
        dates.append({"start_date" : str(v["recommended_date"]).strip(), 
                      "date_range" : str(int(v["date_range"]))})

At end , for the expected output just do

result = {"dates": dates}
azro
  • 53,056
  • 7
  • 34
  • 70
1

Move d_tems = {} inside the for loop so you recreate a new dictionary on each run:

dates = []

for k,v in data.iterrows():
    d_tems = {}     # move here
    if (v["recommended_planting_date"] != "NA"):
        d_tems.update({"start_date":str(v["recommended_date"]).strip()})
        d_tems.update({"date_range":str(int(v["date_range"]))}) 
        print(d_tems)
        dates.append(d_tems)
        # d_tems = {}  # doing this here would work as well, but the former is cleaner 

Your code modifies the one dict because it's declared outside and never reset to a new object.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69