#!/usr/bin/env python import sys, os.path from sys import argv from os import system from string import * if len(argv) < 3: print "Usage: %s rcv1_file id_label_file" % (argv[0]) sys.exit(1) file1 = argv[1] assert os.path.exists(file1),"rcv1_file not found." file2 = argv[2] assert os.path.exists(file2),"id_label_file not found." id_label = map(split, map(strip, open(file2,'r').xreadlines())) dict = {} for list in id_label: dict[list[0]] = list[1:] data = open(file1,'r') for line in data: x = line.split() if dict.has_key(x[0]): ls = dict[x[0]] else: ls = [] print "%s %s"%(','.join(ls), ' '.join(x[1:])) data.close()