-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.py
57 lines (36 loc) · 1.54 KB
/
parse.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
#!user/bin/env python2
import os
import csv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--data", help="path for csv file")
parser.add_argument("--year", help="year")
parser.add_argument("--terms", help="trade terms delimiter is comma")
parser.add_argument("--out", help="path for output data")
args = parser.parse_args()
print args.data
print args.year
print args.terms
track = {}
csvfile = open(args.data, "rb")
creader = csv.DictReader(csvfile)
for row in creader:
if row['Term'] in args.terms.split(','):
if row['Country'] in list(track.keys()):
first_year = int(track.get(row['Country']).get(args.year))
track.update({row['Country']:{args.year: first_year + int(row[args.year]) }})
if row['Term'] in list(track.get(row['Country']).keys()):
existing_value = track.get(row['Country'].get(row['Term']))
track.get(row['Country'].update({row['Term']:existing_value + int(row[args.year])}))
else:
track.get(row['Country'].update({row['Term']:int(row[args.year])}))
continue
track.update({row['Country']:{args.year: int(row[args.year]) }})
track.get(row['Country']).update({row['Term']: int(row[args.year])})
print track
fieldnames = ["id", "population","tusks", "ivory carvings","ivory pieces", "piano keys", "jewellery - ivory", "teeth"]
newcsv = open(args.out, 'wb')
writer = csv.DictWriter(newcsv, fieldnames=fieldnames)
writer.writeheader()
for line in list(track.keys()):
writer.writerow({"id": line, "population": track.get(line).get(args.year), "tusks": track.get(line).get("tusks")})