-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite_csvs.py
63 lines (57 loc) · 2.63 KB
/
composite_csvs.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
from slugify import slugify # awesome-slugify, from requirements
import configuration # configuration.py, with user-defined variables.
import csv
import glob
import time
import datetime
from collections import OrderedDict
import pprint
import os
import sys
from subprocess import Popen
import pickle
import json
datadir = configuration.snapshotsdir
# resultscomposite = configuration.resultscomposite
def composite_csvs():
global resultscomposite
global datadir
lineheaders = ['statename', 'description', 'uncontested', 'polid', 'electiondate',
'precinctsreportingpct', 'winner', 'precinctsreporting', 'incumbent',
'runoff', 'test', 'delegatecount', 'lastupdated', 'racetype', 'polnum',
'votecount', 'electtotal', 'first', 'is_ballot_measure', 'national',
'reportingunitname', 'racetypeid', 'level', 'votepct', 'id', 'last',
'ballotorder', 'officename', 'seatnum', 'seatname', 'precinctstotal',
'raceid', 'electwon', 'party', 'initialization_data', 'candidateid',
'reportingunitid', 'statepostal', 'fipscode', 'officeid']
sourcecsvs = sorted(list(glob.glob(datadir + "*")))
masterlist = []
for filename in sourcecsvs:
newestdirectories = sorted(list(glob.glob(filename +"/*")))
sorted_files = sorted(newestdirectories, key=os.path.getctime)
newestdirectory = sorted_files[-1]
csvs = sorted(list(glob.glob(newestdirectory +"/*.csv")))
for filename in csvs:
if '/snapshots/Florida/' not in filename:
with open(filename, "r") as csvfile:
reader = list(csv.DictReader(csvfile))
# if list(reader[0].keys()) != lineheaders:
# print(list(reader[0].keys()))
# print("CSV input file " + filename + " has different headers than we're looking for. Not importing.")
# else:
# print("CSV input file " + filename + " seems to fit Elex standard. Importing.")
for row in reader:
masterlist.append(row)
with open('resultscomposite.csv', "w", newline="") as compositefile:
writer = csv.DictWriter(compositefile, fieldnames=lineheaders)
writer.writeheader()
writer.writerows(masterlist)
csvfile = open('resultscomposite.csv', 'r')
jsonfile = open('resultscomposite.json', 'w')
data = []
fieldnames = lineheaders
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
data.append(row)
jsonfile.write(json.dumps(data, jsonfile))
composite_csvs()