0

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)
AMC
  • 2,642
  • 7
  • 13
  • 35
gjones
  • 21
  • 5
  • 1
    Welcome. In your loop `city` is `str` and you're trying to process it like `dict`. – Olvin Roght Jun 02 '20 at 20:36
  • Please provide a [mcve], as well as the entire error output. As an aside, you should use a context manager to handle file objects. – AMC Jun 03 '20 at 01:36
  • Does this answer your question? [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – AMC Jun 03 '20 at 01:36

1 Answers1

0

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.

Shamsul Masum
  • 337
  • 1
  • 2
  • 14