[Tutor] how to join two different files

amrita at iisermohali.ac.in amrita at iisermohali.ac.in
Sat Jul 18 08:28:55 CEST 2009


Thankyou sir it is working.....but one more thing i want to ask that if my
file will have entries like:---

fileA and fileB
12         10
13         12
14
15

means if their no. of entries will not match then how to combine them(both
input files have more than one column).

Thanks,
Amrita

>> Maybe you could break that up a bit? This is the tutor list, not a
>> one-liner competition!
>
> rather than one-liners, we can try to create the most "Pythonic"
> solution. below's my entry. :-)
>
> cheers,
> -wesley
>
> myMac$ cat parafiles.py
> #!/usr/bin/env python
>
> from itertools import izip
> from os.path import exists
>
> def parafiles(*files):
>     vec = (open(f) for f in files if exists(f))
>     data = izip(*vec)
>     [f.close() for f in vec]
>     return data
>
> for data in parafiles('fileA.txt', 'fileB.txt'):
>     print ' '.join(d.strip() for d in data)
>
> myMac$ cat fileA.txt
> FileA
> 12
> 15
> 18
> 20
>
> myMac$ cat fileB.txt
> FileB
> 14
> 16
> 18
> 20
> 22
>
> myMac$ parafiles.py
> FileA FileB
> 12 14
> 15 16
> 18 18
> 20 20
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>     http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA



More information about the Tutor mailing list