From: James Mills on
On Sat, May 15, 2010 at 3:22 AM, mannu jha <mannu_0523(a)rediffmail.com> wrote:
> Hi,
>
> I have two different file
>
> file1:
>
> a1 a2
> a3 a4
> a5 a6
> a7 a8
>
> file2:
>
> b1 b2
> b3 b4
> b5 b6
> b7 b8
>
> and I want to join them so the output should look like this:
>
> a1 a2 b1 b2
> a3 a4 b3 b4
> a5 a6 b5 b6
> a7 a8 b7 b8
>
> how to do that?

This is completely untested, but this "should" (tm) work:

from itertools import chain

input1 = open("input1.txt", "r").readlines()
input2 = open("input2.txt", "r").readlines()
open("output.txt", "w").write("".join(chain(input1, input2)))

cheers
James
From: Tim Chase on
On 05/14/2010 12:55 PM, James Mills wrote:
>> file1:
>> a1 a2
>> a3 a4
>> a5 a6
>> a7 a8
>>
>> file2:
>> b1 b2
>> b3 b4
>> b5 b6
>> b7 b8
>>
>> and I want to join them so the output should look like this:
>>
>> a1 a2 b1 b2
>> a3 a4 b3 b4
>> a5 a6 b5 b6
>> a7 a8 b7 b8
>
> This is completely untested, but this "should" (tm) work:
>
> from itertools import chain
>
> input1 = open("input1.txt", "r").readlines()
> input2 = open("input2.txt", "r").readlines()
> open("output.txt", "w").write("".join(chain(input1, input2)))

I think you meant izip() instead of chain() ... the OP wanted to
be able to join the two lines together, so I suspect it would
look something like

# OPTIONAL_DELIMITER = " "
f1 = file("input1.txt")
f2 = file("input2.txt")
out = open("output.txt", 'w')
for left, right in itertools.izip(f1, f2):
out.write(left.rstrip('\r\n'))
# out.write(OPTIONAL_DELIMITER)
out.write(right)
out.close()

This only works if the two files are the same length, or (if
they're of differing lengths) you want the shorter version. The
itertools lib also includes an izip_longest() function with
optional fill, as of Python2.6 which you could use instead if you
need all the lines

-tkc




From: James Mills on
On Sat, May 15, 2010 at 4:46 AM, Tim Chase
<python.list(a)tim.thechases.com> wrote:
> I think you meant izip() instead of chain() ... the OP wanted to be able to
> join the two lines together, so I suspect it would look something like

You're quite right! My mistake :)

--James
From: Tim Chase on
On 05/15/2010 09:20 AM, mannu jha wrote:

BTW: your mailer makes an absolute mess of plain-text emails,
putting multiple spaces



between


every


single


line


which


makes


it


very


hard


to


read.

Please fix it, use a real mailer, or risk getting ignored (or
worse, plonked). Fortunately, Vim makes it modestly easy to
unmung the rubbishy format.

>> # OPTIONAL_DELIMITER = " "
>> f1 = file("input1.txt")
>> f2 = file("input2.txt")
>> out = open("output.txt", 'w')
>> for left, right in itertools.izip(f1, f2):
>> out.write(left.rstrip('\r\n'))
>> # out.write(OPTIONAL_DELIMITER)
>> out.write(right)
>> out.close()
>> This only works if the two files are the same length, or (if
>> they're of differing lengths) you want the shorter version. The
>> itertools lib also includes an izip_longest() function with
>> optional fill, as of Python2.6 which you could use instead if you
>> need all the lines
>
> with this
>
> from itertools import chain
> # OPTIONAL_DELIMITER = " "
> f1 = file("input1.txt")
> f2 = file("input2.txt")
> out = open("output.txt", 'w')
> for left, right in itertools.izip(f1, f2):
> out.write(left.rstrip('\r\n'))
> # out.write(OPTIONAL_DELIMITER)
> out.write(right)
> out.close()
> it is showing error:
> ph08001(a)linux-af0n:~> python join.py
> Traceback (most recent call last):
> File "join.py", line 6, in
> for left, right in itertools.izip(f1, f2):
> NameError: name 'itertools' is not defined

That's because you're not importing itertools, but you're just
importing "chain" from within it. So of course when you try to
use "itertools.izip", itertools doesn't exist. You can either use

import itertools
#...
for left, right in itertools.izip(f1,f2):

or

from itertools import izip
#...
for left, right in izip(f1,f2):

-tkc