I'm using 2.3, I was trying to be as explicit as possible. *grin*

On Thu, 13 Jan 2005 22:02:13 -0500, Jacob S. <[EMAIL PROTECTED]> wrote:
> I assume that both you and Liam are using previous-er versions of python?
> Now files are iterators by line and you can do this.
> 
> openFile = open("probe_pairs.txt","r")
> indexesToRemove = []
> for line in openFile:
>     if line.startswith("Name="):
>         line = ''   ## Ooops, this won't work because it just changes what
> line references or points to or whatever, it doesn't
>                     ##actually change the object
> openFile.close()
> 
> I think this is a great flaw with using for element in list instead of for
> index in range(len(list))
> 
> Of course you later put IMHO better solutions (list comprehensions,etc.) --
> I just wanted to point out that files have been
> iterators for a few versions now and that is being used more and more. It
> also saves the memory problem of using big files
> and reading them all at once with readlines.
> 
> Jacob
> 
> > Liam Clarke wrote:
> >
> > > openFile=file("probe_pairs.txt","r")
> > > probe_pairs=openFile.readlines()
> > >
> > > openFile.close()
> > >
> > > indexesToRemove=[]
> > >
> > > for lineIndex in range(len(probe_pairs)):
> > >
> > >        if probe_pairs[lineIndex].startswith("Name="):
> > >                      probe_pairs[lineIndex]=''
> >
> > If the intent is simply to remove all lines that begin with "Name=",
> > and setting those lines to an empty string is just shorthand for that,
> > it'd make more sense to do this with a filtering list comprehension:
> >
> >      openfile = open("probe_pairs.txt","r")
> >      probe_pairs = openfile.readlines()
> >      openfile.close()
> >
> >      probe_pairs = [line for line in probe_pairs \
> >                            if not line.startswith('Name=')]
> >
> >
> > (The '\' line continuation isn't strictly necessary, because the open
> > list-comp will do the same thing, but I'm including it for
> > readability's sake.)
> >
> > If one wants to avoid list comprehensions, you could instead do:
> >
> >      openfile = open("probe_pairs.txt","r")
> >      probe_pairs = []
> >
> >      for line in openfile.readlines():
> >          if not line.startswith('Name='):
> >              probe_pairs.append(line)
> >
> >      openfile.close()
> >
> > Either way, lines that start with 'Name=' get thrown away, and all
> > other lines get kept.
> >
> > Jeff Shannon
> > Technician/Programmer
> > Credit International
> >
> >
> > _______________________________________________
> > Tutor maillist  -  [email protected]
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> _______________________________________________
> Tutor maillist  -  [email protected]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to