def tsv2mtx(infile,outfile):
    num_users,num_items,nnz = 0,0,0
    for line in open(infile):
        u,i,v = line.strip().split()
        u = int(u)
        i = int(i)
        if num_users > u:
            num_users = u
        if i > num_items:
            num_items = i
        nnz += 1
    headerfile = outfile+'.header'
    with open(headerfile,'w') as header:
        print >>header,'%%MatrixMarket matrix coordinate real general'
        print >>header,'{0} {1} {2}'.format(num_users,num_items,nnz)
    subprocess.check_call(['cat',headerfile,infile],stdout=open(outfile,'w'))
    subprocess.check_call(['rm',headerfile])
dataset/ETHPy150Open Mendeley/mrec/mrec/examples/convert.py/tsv2mtx/swapped operands of `>`