-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountformats.py
More file actions
30 lines (26 loc) · 758 Bytes
/
countformats.py
File metadata and controls
30 lines (26 loc) · 758 Bytes
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
import json
import fileinput
import sys
if sys.argv[len(sys.argv)-1] != sys.argv[0]:
file = open(sys.argv[len(sys.argv)-1], 'w')
file.write("format\t\"total occurrences\"\n")
else:
sys.exit("No filename supplied!")
countr = {}
nrformats = 0
# Open standard input
f = fileinput.input('-')
for line in f:
decoded = json.loads(line)
if "physical_format" in decoded:
k = decoded["physical_format"]
if k in countr.keys():
countr[k] = countr[k] + 1
else:
countr[k] = 1
nrformats = nrformats + 1
print "new format ("+ str(nrformats) +"):", k.encode('utf-8')
keys = countr.keys()
keys.sort()
for k in keys:
file.write(k.encode('utf-8') + "\t{0}\n".format(countr[k]))