Reading data from 2 different files and writing to a single file

inshu chauhan insideshoes at gmail.com
Mon Jan 28 09:47:52 EST 2013


Your current logic tries to scan through the first file, and for each line
that has 12 elements, scans through the entire second file.  It fails to
actually do it, because you never do a seek on the second file.

>
> Now it appears your requirement is entirely different.  I believe you have
> two text files each having the same number of lines.  You want to loop
> through the pair of lines (once from each file, doing some kind of
> processing and printing).  If that's the case, your nested loop is the
> wrong thing, and you can forget my caveat about nesting file reads.
>
> What you want is the zip() function
>
> for l,s in zip(f1, f2):
>     #you now have one line from each file,
>     #   which you can then validate and process
>
> Note, this assumes that when a line is "bad" from either file, you're
> going to also ignore the corresponding line from the other.  If you have to
> accommodate variable misses in the lining up, then your work is *much*
> harder.
>
>
>
>
>
> Actually these are Arff files used in Weka (Data Mining ), So they have a
certain amount of header information which is the same in both files(in
same no. of lines too )  and both files have equal lines, So when I read
basically In both files I am trying to ignore the Header information.

then it is like reading first line from f1 and first line from f2,
extracting the data I want from each file and simply write it to a third
file line by line...

What does actually Zip function do ?

Thanks and Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130128/8a54bc88/attachment.html>


More information about the Python-list mailing list