adding items from a text file to a list

Andrei Kulakov sill at optonline.net
Thu Nov 1 01:34:40 CET 2001


On 30 Oct 2001 16:12:14 -0800, toflat <toflatpy2 at oaktown.org> wrote:
> Hi,
> 
> Sorry if this is a bit of a no-brainer question...
> 
> I have a text file with a list of data items.
> 
> ex:
> 
> pickle	(fruit or veggie?)
> pickled eggs	(just plain strange)
> seinfeld	(funny show)
> yada yada	(yada)
> 
> What I want to do is add each item in the file to a python list. The
> thing that is eluding me is how to leave out the comments. I need it
> to scan in the item, skip anything in parenthsis then proceed to the
> next line.
> 
> Thanks for any help!
> 
> -t

lst = []
for line in lines:
    entries = line.split("(")   # split in two parts, value and comment
    entry = entries[0]          # get the value
    entry = entry.strip()       # get rid of leading and trailing
                                # whitespace
    lst.append(entry)
    


-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org



More information about the Python-list mailing list