简体   繁体   中英

change/update value of a list of dict with python

I'm creating a JSON from some data using append to update the keys because I use some for loop to that.

The model is the following:

for item in met_variables:
    for it, csv in enumerate(all_files):
       if "prec" in item:
            json_data.append(
                {
                    "stationID": station_id,
                    "lat": lat,
                    "lng": lng,
                    "precValue": value
                },
            )
       else:
           "irradValue": value ## here is the problem ##
  • In the met_variables list there are 2 items (prec and irrad).
  • In the all_files list there are some CSV files that I get the data from.

As I depend on the 2 values that are in met_variables, I can't create JSON at once. I need this iteration.

How could I do this?

Your else statement doesn't make sense as written. Are you trying to add the "irradValue" to json_data? if so that would be

for item in met_variables:
    for it, csv in enumerate(all_files):
        if "prec" in item:
            json_data.append(
                {
                    "stationID": station_id,
                    "lat": lat,
                    "lng": lng,
                    "precValue": value
                },
            )
        else:
            json_data.append("irradValue": value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM