{Python}辞書による集計

with open('a.txt', mode='w', encoding='utf-8') as f1:
  f1.write('PJ01,TABLE,T01\n')
  f1.write('PJ01,INDEX,I01\n')
  f1.write('PJ02,VIEW,V01\n')
  f1.write('PJ03,TABLE,T02\n')
  f1.write('PJ01,TABLE,T03\n')
  f1.write('PJ01,TABLE,T04\n')
  f1.write('PJ02,VIEW,V02\n')
  f1.write('PJ01,TABLE,T06\n')
  f1.write('PJ01,TABLE,T05\n')


with open('a.txt', mode='r', encoding='utf-8') as f2:
  lines = f2.readlines()

total = 0
dic1 ={}
for i, line in enumerate(lines):
  key = line.split(',')[0] + "." + line.split(',')[1] 
  total += 1
  if key in dic1:
    dic1[key] = dic1[key] + 1
  else:
    dic1[key] = 1

print ("プロジェクト/種別毎の集計")

for k, v in dic1.items():
  print (k, v)

dic2 ={}
for k, v in dic1.items():
  key = k.split('.')[0]
  if key in dic2:
    dic2[key] = dic2[key] + v
  else:
    dic2[key] = v

print ("プロジェクト毎の集計")

for k, v in dic2.items():
  print (k, v)

print ("TOTAL=" + str(total) )
print("")

for key, val in dic2.items():
  print ("# " + key)
  for k,v in dic1.items():
    pjmei = k.split('.')[0]
    syubetsu =  k.split('.')[1]
    if pjmei == key:
      print( syubetsu + " -> " + str(v) )
  print("")