looping over large csv python
Question: I have a large csv-file(several hundreds of lines) containing following
structure:  
_filename, sitename, servername_

this csv-file contains several doubles, since the servernames are those from a
cluster(always the same couples) and language-aliases for the sitenames(eg.
mijnhuis, myhouse, mamaison)

the result I'd like to have is the following(notice the ; and the , :  
_filename; sitename1, sitename2, sitename3; servername1, servername2,
servername3;_

How could I do this in the most optimized way?

PS: actual code would be nice, but if you can give me directions, I'd be
equally happy.


Answer: Use at your own risk:

    
    
    import collections as c
    
    f = open('input', 'r')
    sites, servers = c.defaultdict(set), c.defaultdict(set)
    files = set()
    for line in f:
        parts = line.split(',')
        fi,site,server = [p.strip() for p in parts]
        files.add(fi)
        sites[fi].add(site)
        servers[fi].add(server)
    
    for f in files:
        print "%s; %s; %s" % (f, ", ".join(sites[f]), ", ".join(servers[f]))
    

