-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
95 lines (77 loc) · 1.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import json
import csv
# def readCsv(file,header=False):
# with open(file,'r') as f:
# lines = f.readlines()
# if header:
# lines.pop(0)
# return [line.strip().split(',') for line in lines]
def readCsv(file,header=False):
result = []
with open(file,'r') as f:
reader = csv.reader(f)
for row in reader:
result.append(row)
return result
# with open('some.csv') as f:
# reader = csv.reader(f)
# for row in reader:
# print(row)
def createJsonCountries(data):
column = [i[4] for i in data]
countries = sorted(uniq(column))
jData = createJson(countries)
return jData
# remove duplicates
def uniq(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (x in seen or seen_add(x))]
def createJson(countries):
jData = {}
for country in countries:
jData[country] = {}
return jData
def extractSpeciesForCountry(country,cData):
sData = []
for row in cData:
if country in row:
sData.append(row)
fData = filterEndangered(sData)
return fData
# return sData
def filterEndangered(sData):
fData = []
for row in sData:
# print row
tl = row[3]
status1 = row[6]
status2 = row[7]
if (tl=='CR' or tl=='EN' or tl=='VU') and status1 == 'Extant' and status2 == 'Native':
fData.append(row)
return fData
def extractThreatTypes(speciesName,tData):
selected = []
for row in tData:
if speciesName in row:
selected.append(row)
threatTypes = []
for row in selected:
threatTypes.append(row[8])
return threatTypes
def writeJson(jData,jsonFile):
json.dump(jData,open(jsonFile,'w'))
if __name__ == "__main__":
countryCSV = 'CSV/02.txt'
threatCSV = 'CSV/01.txt'
cData = readCsv(countryCSV)
tData = readCsv(threatCSV)
jData = createJsonCountries(cData)
for country in jData:
sData = extractSpeciesForCountry(country,cData)
species = {}
speciesNames = [row[2] for row in sData]
for name in speciesNames:
species[name]=sorted(uniq(extractThreatTypes(name,tData)))
jData[country] = species
writeJson(jData,'countries.json')