Joining all rows of a CSV file that have the same 1st column value in Python

后端 未结 3 514
后悔当初
后悔当初 2021-01-03 16:27

I have a CSV file that goes something like this:

[\'Name1\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 16:31

    You should use itertools.groupby:

    t = [ 
    ['Name1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '+'],
    ['Name1', '', '', '', '', '', 'b', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    ['Name2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'a', ''],
    ['Name3', '', '', '', '', '+', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] 
    ]
    
    from itertools import groupby
    
    # TODO: if you need to speed things up you can use operator.itemgetter
    # for both sorting and grouping
    for name, rows in groupby(sorted(t), lambda x:x[0]):
        print join_rows(rows)
    

    It's obvious that you'd implement the merging in a separate function. For example like this:

    def join_rows(rows):
        def join_tuple(tup):
            for x in tup:
                if x: 
                    return x
            else:
                return ''
        return [join_tuple(x) for x in zip(*rows)]
    

提交回复
热议问题