m=open("c1sort.csv", encoding="utf-8").readlines() algus="" naited=[] f2=open("c1naited.csv", "w", encoding="utf-8") for rida in m: rm=rida.split(";") reatyybid=rm[1].split()[1]+rm[2].split()[1] if reatyybid!=algus: pass if len(naited)>0: print(algus, len(naited), file=f2) print("\n".join(sorted(naited)), file=f2) naited=[] algus=reatyybid for naide in rm[3:]: if len(naide.strip())>3: naited.append(naide) if len(naited)>0: print(algus, len(naited), file=f2) print("\n".join(sorted(naited)), file=f2) f2.close()