<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber <span dir="ltr"><<a href="mailto:wlfraed@ix.netcom.com" target="_blank">wlfraed@ix.netcom.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan<br>
<<a href="mailto:insideshoes@gmail.com">insideshoes@gmail.com</a>> declaimed the following in<br>
gmane.comp.python.general:<br>
<div class="im"><br>
> In the code below I am trying to read 2 files f1 and f2 , extract some data<br>
> from them and then trying to write them into a single file that is 'nf'.<br>
><br>
> import cv<br>
> f1 = open(r"Z:\modules\Feature_Vectors_300.arff")<br>
> f2 = open(r"Z:\modules\Feature_Vectors_300_Pclass.arff")<br>
> nf = open(r"Z:\modules\trial.arff", "w")<br>
><br>
><br>
> for l in f1:<br>
>     sp = l.split(",")<br>
<br>
</div>        If you are going to be splitting on commas, you might want to read<br>
up on the csv (comma separate values) module<br></blockquote><div> </div><div>The  csv module has many fuctions but not of much use to me and it makes my programme slower<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class="im"><br>
><br>
>     if len(sp)!= 12:<br>
>         continue<br>
>     else:<br>
<br>
</div>        Given the apparent block structure, you could drop the<br>
continue/else, and more cleanly just use<br></blockquote><div><br></div><div>Yeah, Thats Right  <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<br>
                if len(sp) == 12:<br>
<div class="im">>         ix = sp[0].strip()<br>
>         iy = sp[1].strip()<br>
>         print ix, iy<br>
><br>
>        for s in f2:<br>
<br>
</div>        It's been mentioned that the indentation is wrong here<br></blockquote><div><br></div><div>I dont know why the indentation is wrong ? <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class="im"><br>
>             st = s.split(",")<br>
><br>
</div>        csv module again<br>
<div class="im"><br>
>             if len(st)!= 11:<br>
>                 continue<br>
>             else:<br>
<br>
</div>        I'm tempted to repeat the comment on reversing the conditional BUT<br>
<div class="im"><br>
>                 clas = st[10].strip()<br>
><br>
>              print ix, iy, clas<br>
>              print >> nf, ix, iy, clas<br>
><br>
</div>        The indentation of the print statements is not aligned with the<br>
previous assignment -- the effect is the same however as everything<br>
under the else is executed anyway.<br>
<br>
        But as has also been mentioned, ignoring indentation, the apparent<br>
algorithm you have here is going to process every line of f2 for the<br>
first line of f1 -- and then for later lines in f1 it will find f2 is at<br>
the end of file, and do nothing. If it is supposed to process every line<br>
of f2 for each line of f1, you'll need to rewind f2.<br></blockquote><div><br></div><div>For that I added 'Break' statement as suggested by Chris in above mails.  <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<br>
        If you mean to match one line of f1 with one line of f2, you do not<br>
want nested loops. But now you have to define the behavior if one of the<br>
two files is correct length and the other is not? Do you skip both or<br>
read the next line from the wrong length file? And how will you handle<br>
files with different numbers of records.<br></blockquote><div><br></div><div>Yes , actually my Prog was like this :<br>for l in f1:<br>    sp = l.split(",")<br>    <br>    if len(sp)!= 12:<br>        continue<br>
    else:<br>        ix = sp[0].strip()<br>        iy = sp[1].strip()<br>        <br>    <br>for s in f2:<br>    st = s.split(",")<br><br>    if len(st)!= 11:<br>        continue<br>    else:<br>        clas = st[10].strip()<br>
    <br>    print ix, iy, clas<br>    print >> nf, ix, iy, clas<br>    break <br>        <br><br>f1.close()<br>f2.close()<br>nf.close()<br><br></div><div>I actually dont want nested loops but cant find another way to achieve what I want, But for these files I am sure that they have equal lengths, thats why I am taking the risk of using nested loops.. Can you suggest any different way to go around this problem , which could be flexible and non-errorneous ?<br>
</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span class=""><font color="#888888"><br>
<br>
<br>
--<br>
        Wulfraed                 Dennis Lee Bieber         AF6VN<br>
        <a href="mailto:wlfraed@ix.netcom.com">wlfraed@ix.netcom.com</a>    <a href="HTTP://wlfraed.home.netcom.com/" target="_blank">HTTP://wlfraed.home.netcom.com/</a><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div></div>