-
Notifications
You must be signed in to change notification settings - Fork 0
/
votes-2016.py
68 lines (61 loc) · 1.95 KB
/
votes-2016.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
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import json
import xlsxwriter
data = []
opcineArr = []
opcineUrl = "https://www.izbori.ba/cik_web_api/race9_electoralunit/%22WebResult_2016MUNI_2016_9_23_16_38_25%22/1"
vjece = "https://www.izbori.ba/cik_web_api/race9_electoralunitpartyresult/%22WebResult_2016MUNI_2016_9_23_16_38_25%22/"
responseOpcine = urlopen(opcineUrl)
opcine = responseOpcine.read().decode("utf-8")
opcineData = json.loads(opcine)
#print(opcineData)
for op in opcineData:
#print(op)
code = op['code']
opcinaName = op['name']
url = vjece + code+"/1"
response = urlopen(url)
data1 = response.read().decode("utf-8")
d = json.loads(data1)
#print(d)
d.append({'municipality':opcinaName})
print(d)
data.append(d)
#for i in range(1,3):
# url = vjece + str(i)+"/1"
# response = urlopen(url)
# data1 = response.read().decode("utf-8")
# d = json.loads(data1)
# print(d)
# data.append(d)
print(data)
workbook = xlsxwriter.Workbook('2016.xlsx')
worksheet = workbook.add_worksheet()
# Write some data headers.
bold = workbook.add_format({'bold': 1})
worksheet.write('A1', 'OPCINA', bold)
worksheet.write('B1', 'STRANKA', bold)
worksheet.write('C1', 'GLASOVI', bold)
worksheet.write('D1', 'MANDATI', bold)
# Start from the first cell. Rows and columns are zero indexed.
row = 1
col = 0
# Iterate over the data and write it out row by row.
for item in (data):
#print(item)
#print(item[0]['name'])
for stranka in item:
print(stranka)
worksheet.write(row, col,stranka['municipality'])
worksheet.write(row, col + 1, stranka['name'])
worksheet.write(row, col + 2, stranka['totalVotes'])
worksheet.write(row, col + 3, stranka['mandates'])
row += 1
workbook.close()
#with open('data2008.json', 'wb') as outfile:
# json.dumps(data, outfile)