简体   繁体   中英

Multiple python lists to json file

I am having 6 python lists as follows:

dows=['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
dates=['10-4', '10-5', '10-6', '10-7', '10-8']
maxs=['24', '23', '17', '16', '18']
mins=['13', '13', '11', '10', '7']
precipitation_probabilities=['0', '0', '76', '70', '21']
wind_speeds=['13', '21', '16', '8', '11']

and like to convert them to json, which I want eventually save to a drive as a file (on rasppberry pi) in format as follows:

{
    "dows":['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
    "dates":['10-4', '10-5', '10-6', '10-7', '10-8'],
    "maxs":['24', '23', '17', '16', '18'],
    "mins":['13', '13', '11', '10', '7'],
    "precipitation_probabilities":['0', '0', '76', '70', '21'],
    "wind_speeds":['13', '21', '16', '8', '11']
}

I've tried everything without luck.

IIUC, you just want the following python dict :

my_dict = {"dows": dows, 
           "dates": dates, 
           "maxs": maxs, 
           "mins": mins, 
           "precipitation_probabilities": precipitation_probabilities,
           "wind_speeds": wind_speeds}

To get the json string representation of the above dictionary:

import json
my_json = json.dumps(my_dict)

To write the dictionary to a json file:

with open('output.json', 'w') as outfile:
    json.dump(my_dict, outfile)

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