Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A Python version could look this:</p> <pre><code>fobj_in = io.StringIO("""Name1, Surname1 Team1 Team2 Team3 Name2, Surname2 Team2 Team4 Name3, Surname3 Team1 Team5""") fobj_out = io.StringIO() from collections import defaultdict teams = defaultdict(list) for line in fobj_in: items = line.split() if len(items) == 3: name = items[:2] team = items[2] else: team = items[0] teams[team].append(name) for team_name in sorted(teams.keys()): fobj_out.write(team_name + ', ') for name in teams[team_name][:-1]: fobj_out.write('{} {}, '.format(name[0], name[1])) name = teams[team_name][-1] fobj_out.write('{} {}\n'.format(name[0], name[1])) fobj_out.seek(0) print(fobj_out.read()) </code></pre> <p>Output:</p> <pre><code>Team1, Name1, Surname1, Name3, Surname3 Team2, Name1, Surname1, Name2, Surname2 Team3, Name1, Surname1 Team4, Name2, Surname2 Team5, Name3, Surname3 </code></pre> <p>Just do this to read from and write to an actual file:</p> <pre><code>fobj_in = open('in_file.txt') fobj_out = open('out_file.txt', 'w') </code></pre> <h1>EDIT</h1> <p><em>Note</em>: The sample data seem to contain no case that woud result in multiple names on one line in the output.</p> <p>With <a href="https://www.dropbox.com/s/sl3tu7m77gei987/sample.txt" rel="nofollow">this input data</a>, we need to change the code:</p> <pre><code>from collections import defaultdict teams = defaultdict(list) for line in fobj_in: if not line.strip(): continue items = [entry.strip() for entry in line.split('\t') if entry] if len(items) == 2: name = items[0] team = items[1] else: team = items[0] teams[team].append(name) for team_name in sorted(teams.keys()): fobj_out.write(team_name + ', ') for name in teams[team_name][:-1]: fobj_out.write('{}, '.format(name)) name = teams[team_name][-1] fobj_out.write('{}\n'.format(name)) </code></pre> <p>The resulting file content looks like this:</p> <pre><code>"Décore ta vie" (2003), Boilard, Naggy "Mouki" (2010), Boileau, Sonia A chacun sa place (2011), Boinem, Victor Emmanuel Absence (2009) (V), Boillat, Patricia C.A.L.L.E. (2005), Boillat, Patricia Comment devenir un trou de cul et enfin plaire aux femmes (2004), Boire, Roger Couleur de peau: Miel (2012), Boileau, Laurent Hergé:Les aventures de Tintin (2004), Boillot, Olivier Isola, là dove si parla la lingua di Bacco (2011) (co-director), Boillat, Patricia L'île (2011), Boillot, Olivier La beauté fatale et féroce... (1996), Boire, Roger Last Call Indian (2010), Boileau, Sonia Le Temple Oublié (2005), Boillot, Olivier Le pied tendre (1988), Boire, Roger Legit (2006), Boinski, James W. Nubes (2010), Boira, Francisco Questions nationales (2009), Boire, Roger Reconciling Rwanda (2007), Boiko, Patricia Soviet Gymnasts (1955), Boikov, Vladimir The Corporal's Diary (2008) (V) (head director), Boiko, Patricia Un gars ben chanceux (1977), Boire, Roger </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload