two file into a single file

Tim Chase python.list at tim.thechases.com
Mon Oct 29 16:58:23 CET 2007


>                I have a two file,
>  file 1:
> 100007097
> 100007186
> 10000723
> 100007895
> 100007906
> 100008295
> 100008311
> 10000880
> 100009160
> 100009629
> 
> file 2:
> 100007097
> 100007186
> 10000723
> 100007895
> 100007906
> 100008295
> 100008311
> 10000880
> 100009160
> 100009629
> how do i make into a single file......like this
> file 1             file 2
> 100007097  100007097
> 100007186  100007097
> 100007186   10000880
> 10000723    100007895
> 100007895  100007895
> 100007906  100007895
> 100008295  00008311
> 100008311  1000088

You can use the tool designed for the job:

   bash$ paste file1.txt file2.txt

Or, if you want to use Python you might be interested in

   for a,b in zip(file('file1.txt'), file('file2.txt')):
     print '%s\t%s' % (a.strip(), b.strip())

If they're big, import itertools.izip() and use it instead of zip()

Variants may be needed depending on the behavior if file1 and 
file2 have different numbers of lines in them.

-tkc






More information about the Python-list mailing list