在Python中组合多个值

时间:2015-09-03 19:07:34

标签: python-2.7

我有以下格式的数据(在csv文件中):

 id, review
 1, the service was great!
 1, staff was friendly.
 2, nice location
 2, but the place was not clean
 2, the motel was okay
 3, i wouldn't stay there next time
 3, do not stay there

我想将数据更改为以下格式:

 1, the service was great! staff was friendly. 
 2, nice location but the place was not clean the motel was okay
 3, i wouldn't stay there next time do not stay there

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用itertools.groupby对具有相同编号的连续条目进行分组。

import itertools, operator, csv
with open("test.csv") as f:
    reader = csv.reader(f, delimiter=",")
    next(reader) # skip header line
    for key, group in itertools.groupby(reader, key=operator.itemgetter(0)):
        print key, ' '.join(g[1] for g in group)

输出:

1  the service was great!  staff was friendly.
2  nice location  but the place was not clean  the motel was okay
3  i wouldn't stay there next time  do not stay there

注意:读取文件的代码假设它是一个实际的CSV文件,带有,分隔符:

id, review
1, the service was great!
...