I have a CSV file that goes something like this:
['Name1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '+']
['Name1', '', '', '', '', '', 'b', '', '',
'', '', '', '', '', '', '', '', '', '', '', '']
['Name2', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'a',
'']
['Name3', '', '', '', '', '+', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '']
Now, I need a way to join all of the rows that have the same 1st column name into one column, for instance:
['Name1', '', '', '', '', '', 'b', '', '', '', '', '', '', '', '', '',
'', '', '', '', '+']
['Name2', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'a',
'']
['Name3', '', '', '', '', '+', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '']
I can think of a way to do this by sorting the CSV and then going trough each row and column and compare each value, but there should probably be an easier way to do it.
Any ideas?
解决方案
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)]