简体   繁体   中英

Write a json file from list

I have the following list data I want to save in a json file to be access later:

data = [{"nomineesWidgetModel":{"title":"","description":"",  
"refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]

If saved as txt:

for item in data:
    with open('./data/awards.txt', 'w', encoding='utf-8') as f:
        f.write(', '.join(str(item) for item in data))

Output:
{"nomineesWidgetModel":{"title":"","description":"","refMarker":"ev_nom", 
 "eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}

But I get an error when opening the file later in Jupyter Notebook

If save as json

for item in data:
    with open('data.json', 'w', encoding='utf-8') as f:
        json.dump(item, f, ensure_ascii=False, indent=4)

Output with extra backslash:
"{\"nomineesWidgetModel\":{\"title\":\"\",\"description\":\"\",\"refMarker\":\"ev_nom\", 
 \"eventEditionSummary\":{\"awards\":[{\"awardName\":\"Oscar\",\"trivia\":[],}]}}

Is there a simpler way to do this without having to import the file and replace the extra slashes?

Thanks to @Alexander explanation above, I was able to save the content I was scraping in a dict, and not a list, and then save as json while iterating the pages with:

with open('data.json', 'a') as file:
            json.dump(data, file, indent=1)

Just use json as usual:

import json

data = [{"nomineesWidgetModel":{"title":"","description":"", "refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]

with open('data.json', 'w') as f:
    json.dump(data, f, indent=4)

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