Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import sys
- def isin(string, lst):
- return bool((True in [string in x for x in lst]) or (True in [x in string for x in lst]))
- fname = "rawdata.json"
- excluded_regions = ["Jammu", "Agartala", "Imphal", "JK", "J&K", "Kashmir", "Jammu & Kashmir", "Pampore", "Charari Sharief", "Manipur", "Tripura", "Assam", "Arunachal", "Meghalaya", "Mizoram", "Nagaland", "Chhattisgarh"]
- included = [52] # Attack no. 52 (2008 Agartala bombings) were non-insurgent attacks in NE
- excluded = [89] # Attack no. 89 (Pathankot attack) because Pathankot is on the J&K-Punjab-Pak border, hence an insurgent area
- year_wise = {}
- with open(fname, "r") as f:
- rawdata = json.load(f)
- for incident in rawdata:
- try:
- if not ((isin(incident["Location"], excluded_regions) or int(incident["Number"]) in excluded)
- and int(incident["Number"]) not in included):
- year = int(incident["Date"][-4:])
- deaths = incident["Persons Dead"]
- if deaths == "":
- deaths = "0"
- deaths = deaths.replace("+", "")
- deaths = deaths.split(" ")[0] # exclude things like notes etc in the deaths figure
- if "-" in deaths:
- # average
- deaths = sum(([int(x) for x in deaths.split("-")]))//2
- deaths = int(deaths)
- injuries = incident["Injured"]
- if injuries == "":
- injuries = "0"
- injuries = injuries.split(" ")[0] # exclude things like notes etc in the injuries figure
- injuries = injuries.replace("+", "")
- if "-" in injuries:
- # average
- injuries = sum(([int(x) for x in injuries.split("-")]))//2
- injuries = int(injuries)
- dataobj = {"injuries": injuries, "deaths": deaths}
- if year not in year_wise:
- year_wise[year] = []
- year_wise[year].append(dataobj)
- except TypeError:
- print(incident, file=sys.stderr)
- print("CASUALTIES (deaths+injuries)")
- for year in year_wise:
- print(year, sum([x['injuries'] + x['deaths'] for x in year_wise[year]]))
- print("\n\n")
- print("INCIDENTS")
- for year in year_wise:
- print(year, len(year_wise[year]))
Add Comment
Please, Sign In to add comment