Guest User

Untitled

a guest
Apr 30th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import json
  2. import sys
  3.  
  4. def isin(string, lst):
  5.     return bool((True in [string in x for x in lst]) or (True in [x in string for x in lst]))
  6.  
  7.  
  8. fname = "rawdata.json"
  9.  
  10. excluded_regions = ["Jammu", "Agartala", "Imphal", "JK", "J&K", "Kashmir", "Jammu & Kashmir", "Pampore", "Charari Sharief", "Manipur", "Tripura", "Assam", "Arunachal", "Meghalaya", "Mizoram", "Nagaland", "Chhattisgarh"]
  11.  
  12. included = [52]  # Attack no. 52 (2008 Agartala bombings) were non-insurgent attacks in NE
  13.  
  14. excluded = [89]  # Attack no. 89 (Pathankot attack) because Pathankot is on the J&K-Punjab-Pak border, hence an insurgent area
  15.  
  16. year_wise = {}
  17.  
  18. with open(fname, "r") as f:
  19.     rawdata = json.load(f)
  20.  
  21. for incident in rawdata:
  22.     try:
  23.         if not ((isin(incident["Location"], excluded_regions) or int(incident["Number"]) in excluded)
  24.                  and int(incident["Number"]) not in included):
  25.             year = int(incident["Date"][-4:])
  26.            
  27.             deaths = incident["Persons Dead"]
  28.             if deaths == "":
  29.                 deaths = "0"
  30.             deaths = deaths.replace("+", "")
  31.             deaths = deaths.split(" ")[0]  # exclude things like notes etc in the deaths figure
  32.             if "-" in deaths:
  33.                 # average
  34.                 deaths = sum(([int(x) for x in deaths.split("-")]))//2
  35.             deaths = int(deaths)
  36.            
  37.             injuries = incident["Injured"]
  38.             if injuries == "":
  39.                 injuries = "0"
  40.             injuries = injuries.split(" ")[0]  # exclude things like notes etc in the injuries figure
  41.             injuries = injuries.replace("+", "")
  42.             if "-" in injuries:
  43.                 # average
  44.                 injuries = sum(([int(x) for x in injuries.split("-")]))//2
  45.             injuries = int(injuries)
  46.            
  47.             dataobj = {"injuries": injuries, "deaths": deaths}
  48.             if year not in year_wise:
  49.                 year_wise[year] = []
  50.             year_wise[year].append(dataobj)
  51.     except TypeError:
  52.         print(incident, file=sys.stderr)
  53.  
  54. print("CASUALTIES (deaths+injuries)")
  55. for year in year_wise:
  56.     print(year, sum([x['injuries'] + x['deaths'] for x in year_wise[year]]))
  57. print("\n\n")
  58. print("INCIDENTS")
  59. for year in year_wise:
  60.     print(year, len(year_wise[year]))
Add Comment
Please, Sign In to add comment