<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 30, 2013 at 2:23 PM, Dave Angel <span dir="ltr"><<a href="mailto:d@davea.name" target="_blank">d@davea.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On 01/30/2013 05:43 AM, inshu chauhan wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber <<a href="mailto:wlfraed@ix.netcom.com" target="_blank">wlfraed@ix.netcom.com</a>>wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan<br>
<<a href="mailto:insideshoes@gmail.com" target="_blank">insideshoes@gmail.com</a>> declaimed the following in<br>
gmane.comp.python.general:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In the code below I am trying to read 2 files f1 and f2 , extract some<br>
</blockquote>
data<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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_<u></u>Vectors_300.arff")<br>
f2 = open(r"Z:\modules\Feature_<u></u>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>
</blockquote>
<br>
If you are going to be splitting on commas, you might want to read<br>
up on the csv (comma separate values) module<br>
<br>
</blockquote>
<br>
The csv module has many fuctions but not of much use to me and it makes my<br>
programme slower<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
if len(sp)!= 12:<br>
continue<br>
else:<br>
</blockquote>
<br>
Given the apparent block structure, you could drop the<br>
continue/else, and more cleanly just use<br>
<br>
</blockquote>
<br>
Yeah, Thats Right<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
if len(sp) == 12:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
ix = sp[0].strip()<br>
iy = sp[1].strip()<br>
print ix, iy<br>
<br>
for s in f2:<br>
</blockquote>
<br>
It's been mentioned that the indentation is wrong here<br>
<br>
</blockquote>
<br>
I dont know why the indentation is wrong ?<br>
</blockquote>
<br></div></div>
Your for statement is not lined up with the print that precedes it. If your code were really that way, you'd be getting an indentation error. So we assume it's because your email editor is mangling the code. Post in text email, not in html.<div>
<div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
st = s.split(",")<br>
<br>
</blockquote>
csv module again<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
if len(st)!= 11:<br>
continue<br>
else:<br>
</blockquote>
<br>
I'm tempted to repeat the comment on reversing the conditional BUT<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
clas = st[10].strip()<br>
<br>
print ix, iy, clas<br>
print >> nf, ix, iy, clas<br>
<br>
</blockquote>
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>
<br>
</blockquote>
<br>
For that I added 'Break' statement as suggested by Chris in above mails.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;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>
<br>
</blockquote>
<br>
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>
</blockquote>
<br></div></div>
This is not nested, it's back at the left margin. Or it could be posting wrong because you're still posting in html, instead of plain text email.</blockquote><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">
<div class="im">Yes My Initial code was not nested at the same time of no use too, I am trying to use zip() now :) :) nyways..<br></div><div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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>
I actually dont want nested loops but cant find another way to achieve what<br>
I want, But for these files I am sure that they have equal lengths, thats<br>
why I am taking the risk of using nested loops.<br>
</blockquote>
<br></div>
You have that backwards. Because you say you can assume they're the same length, you don't need the flexibility (and unreadability) of the nested approach. The zip approach works great, and nested is unnecessary.<div class="im">
<br>
<br>
. Can you suggest any<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
different way to go around this problem , which could be flexible and<br>
non-errorneous ?<br>
<br>
<br>
</blockquote>
<span class="HOEnZb"><font color="#888888"><br>
</font></span></div></blockquote></div><br></div></div>