m=open("C1_trigrammid.csv", encoding="utf-8").readlines() f2=open("C1_naited.txt", "w", encoding="utf-8") def jarjestus(rida): ms=rida.split(";") if len(ms[1].split())<3: return "XXX" return ms[1].split()[1]+ms[2].split()[1]+ms[3].split()[1] m.sort(key=jarjestus) #print(*m) jarjend="" hoidla=[] for rida in m: j=jarjestus(rida) if j!=jarjend and hoidla: kogus=0 naited=[] for h in hoidla: m=h.split(";") if m[0]=="xxx": continue kogus+=int(m[0]) for naide in m[4:]: if naide.strip(): naited.append(naide.strip()) print(jarjend, kogus, file=f2) naited.sort() for naide in naited: print(" ", naide, file=f2) hoidla=[] hoidla.append(rida) jarjend=j #print(jarjend) f2.close()