# -*- coding: utf8 -*-
import csv, itertools, json
def cluster(rows):
result = []
data = sorted(rows, key=lambda r: r[1])
for k, g in itertools.groupby(rows, lambda r: r[0]):
group_rows = [row[1:] for row in g]
if len(row[1:]) == 1:
result.append({"round": row[0],"winner": int(row[1]),"points": int(row[2]),"year": int(row[3])})
else:
result.append({"round": k,"children":cluster(group_rows)})
return result
if __name__ == '__main__':
s = '''\
Final Rafael Nadal 7-5 3-6 7-6(3) 3-6 6-2 2009
semi Rafael Nadal 6-7(4) 6-4 7-6(2) 6-7(1) 6-4 2009
semi Roger Federer 6-2 7-5 7-5 2009
quarter Rafael Nadal 6-2 7-5 7-5 2009
quarter Fernando Verdasco 7-6(2) 3-6 6-3 6-2 2009
quarter Roger Federer 6-3 6-0 6-0 2009
quarter Andy Roddick 6-7(3) 6-4 6-2 2-1 ret. 2009
Fourth Rafael Nadal 6-3 6-2 6-4 2009
Fourth Jo-Wilfried Tsonga 6-4 6-4 7-6(3) 2009
Fourth Gilles Simon 6-4 2-6 6-1 ret. 2009
Fourth Fernando Verdasco 2-6 6-1 1-6 6-3 6-4 2009
Fourth Juan Martin Del Potro 5-7 6-4 6-4 6-2 2009
Fourth Andy Roddick 7-5 6-1 6-3 2009
Fourth Roger Federer 4-6 6-7(4) 6-4 6-4 6-2 2009
Fourth Novak Djokovic 6-1 7-6(1) 6-7(5) 6-2 2009
Third James Blake 6-3 6-2 3-6 6-1 2009
Third Fernando Gonzalez 3-6 3-6 7-6(10) 6-2 12-10 2009
Third Gael Monfils 6-4 6-3 7-5 2009
Third Fernando Verdasco 6-4 6-0 6-0 2009
Third Jo-Wilfried Tsonga 6-4 6-2 1-6 6-1 2009
Third Andy Murray 7-5 6-0 6-3 2009
Third Rafael Nadal 6-4 6-2 6-2 2009
Third Gilles Simon 7-6(2) 6-4 6-2 2009
Third Tommy Robredo 6-1 6-3 6-2 2009
Third Andy Roddick 6-3 6-4 6-2 2009
Third Marin Cilic 7-6(5) 6-3 6-4 2009
Third Marcos Baghdatis 6-2 6-4 6-4 2009
Third Tomas Berdych 4-6 6-1 6-3 6-4 2009
Third Novak Djokovic 6-2 4-6 6-3 7-6(4) 2009
Third Roger Federer 6-3 6-2 7-6(5) 2009
Third Juan Martin Del Potro 6-7(5) 7-5 6-3 7-5 2009
Second Radek Stepanek 6-3 6-2 6-7(3) 7-5 2009
Second Richard Gasquet 6-3 6-4 6-4 2009
Second Nicolas Almagro 6-2 7-5 6-0 2009
Second Andy Murray 6-4 6-2 6-2 2009
Second James Blake 6-3 6-2 6-3 2009
Second Jurgen Melzer 5-7 7-6(7) 6-4 6-3 2009
Second Mario Ancic 5-7 7-5 4-6 6-4 6-3 2009
Second Igor Andreev 6-4 6-4 5-7 3-6 6-4 2009
Second Fernando Verdasco 6-1 6-1 6-2 2009
Second Dudi Sela 6-3 6-3 6-2 2009
Second Tommy Haas 6-1 6-2 6-1 2009
Second Gilles Simon 6-7(5) 6-4 6-1 6-2 2009
Second Jo-Wilfried Tsonga 6-7(4) 7-6(8) 7-6(7) 6-2 2009
Second Fernando Gonzalez 7-5 6-3 6-4 2009
Second Rafael Nadal 6-2 6-3 6-2 2009
Second Gael Monfils 6-4 6-4 3-6 6-2 2009
Second Marat Safin 7-5 6-2 6-2 2009
Second Mardy Fish 6-4 6-1 7-5 2009
Second Tomas Berdych 6-1 6-1 6-3 2009
Second Stanislas Wawrinka 6-3 6-4 6-4 2009
Second Marcos Baghdatis 3-6 7-5 6-3 6-3 2009
Second David Ferrer 6-2 6-2 6-1 2009
Second Novak Djokovic 7-5 6-1 6-3 2009
Second Tommy Robredo 6-1 6-3 6-0 2009
Second Andy Roddick 4-6 6-2 7-6(1) 6-2 2009
Second Fabrice Santoro 5-7 7-5 3-6 7-5 6-3 2009
Second Roger Federer 6-2 6-3 6-1 2009
Second Amer Delic 1-6 3-6 6-3 7-6(3) 9-7 2009
Second Yen-Hsun Lu 6-4 5-7 4-6 6-4 6-2 2009
Second Gilles Muller 3-6 6-1 6-4 6-2 2009
Second Marin Cilic 6-2 6-3 4-6 6-3 2009
Second Juan Martin Del Potro 6-1 7-5 6-2 2009
First James Blake 6-4 6-3 7-5 2009
First Roko Karanusic 6-3 1-6 6-3 3-6 6-3 2009
First Fernando Gonzalez 5-7 6-2 6-2 3-6 6-3 2009
First Andy Murray 6-2 3-1 ret. 2009
First Radek Stepanek 3-6 6-3 6-4 6-4 2009
First Victor Hanescu 3-6 3-6 7-6(7) 7-6(4) 8-6 2009
First Stefan Koubek 6-3 6-2 6-2 2009
First Ernests Gulbis 6-3 6-2 6-3 2009
First Mario Ancic 7-5 6-4 4-6 6-2 2009
First Fernando Verdasco 6-0 6-2 6-2 2009
First Arnaud Clement 6-3 2-6 4-6 6-2 6-1 2009
First Michael Berrer 6-2 6-4 6-3 2009
First Andreas Beck 7-5 6-1 6-0 2009
First Tommy Haas 6-3 6-3 6-4 2009
First Denis Istomin 6-2 7-5 6-4 2009
First Gael Monfils 6-1 6-3 7-5 2009
First Igor Andreev 5-7 3-6 6-4 6-3 6-4 2009
First Sebastien de Chaunac 2-6 6-3 0-6 6-2 6-2 2009
First Ivan Ljubicic 4-6 7-6(3) 7-6(7) 5-7 6-3 2009
First Fabio Fognini 3-6 7-6(7) 6-4 6-2 2009
'''
rows = list(csv.reader(s.splitlines()))
print json.dumps(cluster(rows),indent=2)