zip() is your friend. It turns rows into columns and columns into rows.

I think this will do what you want, assuming all the first columns are identical (no missing rows in any files), the row values are separated by spaces or tabs, and all the data will fit in memory:

def readColumns(filePath):
    rows = [ line.split() for line in open(filePath) ]
    return zip(*rows)

# list of all the files to read
allFiles = [ 'f1.txt', 'f2.txt' ]

# both columns from all files
allColumns = [ readColumns(filePath) for filePath in allFiles ]

# just the second column from all files
allSecondColumns = [ cols[1] for cols in allColumns ]

# a representative first column
col1 = allColumns[0][0]

# zip it up into rows
allRows = zip(col1, *allSecondColumns)

for row in allRows:
    print '\t'.join(row)


Kent

kumar s wrote:
Hello:

In append function instead of appending one below the
other can I append one next to other.


I have a bunch of files where the first column is
always the same. I want to collect all those files,
extract the second columns by file wise and write the
first column, followed by the other columns(extracted
from files) next to each other.

Any tricks , tips and hints.

thanks
K






__________________________________ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. http://promotions.yahoo.com/new_mail
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to