Need help removing list elements.

N

nuffnough

This is python 2.4.3 on WinXP under PythonWin.

I have a config file with many blank lines and many other lines that I
don't need.

read the file in, splitlines to make a list, then run a loop that
looks like this:



config_file = open("lines.txt", "rb")
returned_lines = config_file.read().splitlines()

i = len(returned_lines)


for i in range(i):
if returned_lines.find("Value") == -1:
if returned_lines.find("Name") == -1:
print "read in this useless line ..."
print returned_lines
print "Removing line ..."
returned_lines = ""




This blanks out all the lines I don't want. I did originally try 'del
returned_lines' but I got list index out of range, so I made a loop
to delete the empty elements.

for i in range(i):
if returned_lines == "":

del returned_lines



But this gives me "IndexError: list out of range

After much experimentation and dumping of the list, I have figured out
that it doesn't like removing multiple empty elements in a row. In
other words, if there are 4 empty lines, it will remove one of them,
and seems to behave as though that was one element instead of 3. if I
make i = i - *number of groups of empty elements* it works without an
error, but leaves many empty elements behind.

Obviously I can iterate over it time and again, but that isn't how the
world should work.

Is this something obvious that I am doing wrong, or something more
complicated?

Any help will be gratefully appreciated!
 
J

jwelby

This looks like a job for list comprehensions:
returned_lines= ['Name: John, Value: 12','We don't want this one.','Name: Eric, Value: 24']
[x for x in returned_lines if ('Name' in x and 'Value' in x)]
['Name: John, Value: 12', 'Name: Eric, Value: 24']

List comprehensions are great. If you are not familiar with them, check
out the Python documentation. Once you get started with them, you won't
look back.
 
J

jwelby

Ooops!

Looking at your example a bit closer, change the 'and' in the list
comprehension I posted to 'or', and it should do what you want.
 
M

Max Erickson

(e-mail address removed) wrote in

But this gives me "IndexError: list out of range

You are making the list shorter as you are iterating. By the time your
index is at the end of the original list, it isn't that long any more.
Creating a new list and appending the elements you want to keep avoids
the problem. Or you can just use a list comprehension(untested):

returned_lines=[line for line in open("lines.txt", 'rb')
if line != ""]

or just

returned_lines=[line for line in open("lines.txt") if line]

max
 
J

John Machin

(e-mail address removed) wrote in



You are making the list shorter as you are iterating. By the time your
index is at the end of the original list, it isn't that long any more.

If you are hell-bent on conditionally deleting items from a list in
situ, you need to do it backwards:

for i in xrange(len(alist)-1, -1, -1):
if not_interested(alist):
del alist
Creating a new list and appending the elements you want to keep avoids
the problem. Or you can just use a list comprehension(untested):

returned_lines=[line for line in open("lines.txt", 'rb')

Call me crazy, but I wouldn't open the file in BINARY mode :)
if line != ""]

or just

returned_lines=[line for line in open("lines.txt") if line]

For a modicum of extra effort, the condition "if line.strip()" throws
away lines containing only whitespace.

However I don't see the point of creating a list of lines, then throwing
out only *some* of the uninteresting ones. IMHO the OP might be better
advised to read the file one line at a time, ignoring
blank/empty/comment lines, then *validate* the remainder. Hint: with the
semi-squished-list approach, you can't report the original line number
of any erroneous line without extra effort.

The OP might be even better advised to (read the source of, use) an
existing config file parser module.

Hope some of this helps,
John
 
N

nuffnough

Thanks to all those who responded. It has all helped immensely. :)

I didn't know about list comprehensions before I started.

very warm regards to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top