-
Notifications
You must be signed in to change notification settings - Fork 2
/
readport.py
65 lines (53 loc) · 1.3 KB
/
readport.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
#!/usr/bin/env python
"""
readport.py
"""
import csv
# A function that reads a file into a list of dicts
def read_portfolio(filename):
portfolio = []
with open(filename) as f:
f_csv = csv.reader(f)
headers = next(f_csv)
for row in f_csv:
record = {
'name' : row[0],
'shares' : int(row[1]),
'price' : float(row[2])
}
portfolio.append(record)
return portfolio
portfolio = read_portfolio('Data/portfolio.csv')
from pprint import pprint
pprint(portfolio)
"""
#count total number of shares in portfolio
from collections import Counter
totals=Counter()
for s in portfolio:
totals[s['name']] += s['shares']
print(totals['IBM'])
print(totals['MSFT'])
print(totals.most_common(3))
#combining portfolios
portfolio2 = read_portfolio('Data/portfolio2.csv')
totals2 = Counter()
for s in portfolio2:
totals2[s['name']] += s['shares']
print(totals)
print(totals2)
combined = totals + totals2
print(combined)
#group data by stock name
from collections import defaultdict
byname = defaultdict(list)
for s in portfolio:
byname[s['name']].append(s)
print(byname['IBM'])
print(byname['AA'])
#initialize and append at the same time
d = defaultdict(list)
print(d)
print(d['x'])
print(d['y'])
"""