I'm trying to count the number of national holidays N
in each city in a .json
file and I keep getting the error TypeError: string indices must be integers
.
I've read a bunch of other posts about this error and I can't really find anything that helps. I'm assuming its related to how I'm pulling the .json
file? (encoding for non-english letters in file)
import json
world_hol_data = json.load(open("holiday_data.json", "r", encoding="utf8"))
total_national_hol = 0
for cont in world_hol_data:
for city in cont:
if city["type"] == "N":
total_national_hol += 1
print(total_national_hol)
Please first check your data variable to see what you received there. I guess at least a list containing with string whereas it should be a list of dictionaries.
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.