[Tutor] how to join two different files

wesley chun wescpy at gmail.com
Sat Jul 18 09:46:32 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).

this is up to you and what type of solution you want to implement.
rich (see below) has a great idea that you can use to address your
situation.

one thing that is *very* important is to not just copy a working
solution that tutors have posted, but to truly study each snippet of
code to fully understand exactly how every line works, because you
will have to come up with solutions on your own at some point, so this
should be a good lesson to learn.

so far, several examples have been posted, but you should compare and
contrast them, and decide for yourself which ones you like, which ones
you don't, and why (or why not). you will become a much better Python
programmer in a shorter period of time! the tutors are here to help
get you jumpstarted with the language, and not necessarily to always
provide you with an answer.


> That depends entirely on what you want to happen when you encounter a
> row that doesn't have an entry in one of the files.  The previous
> code, using izip(), will stop at the end of the shortest file.  If you
> want to pad the short file, (i.e. add values to make it the same
> length as the longer file) you can use itertools.izip_longest().  Look
> it up in the docs for usage.

excellent idea... i would highly encourage everyone to study this
module. it has breaktaking Python code, much in line with snippets
you'd find in the Cookbook, and yes, i expose students to this in my
intro Python course!

http://docs.python.org/library/itertools.html

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2009
http://withdjango.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list