extract element

Jay Dorsey jay at jaydorsey.com
Sat May 31 13:54:37 EDT 2003


Darren,

How about this:

>>> # create a multi-line string
>>> mystr = 'this:is:something\nblah:blah:blah\nhere:is:more'  
>>> # save it to a text file (simulates the file you already have)
>>> f = file('myfile.txt', 'w').write(mystr)
>>> mylist = []  # create a list to hold the items you want
>>> # open the text file up
>>> f = file('myfile.txt', 'r')
>>> for line in f.readlines():  # go over each line
...   # split on the : and grab the second element
...   mylist.append(line.split(':')[1])  
...
>>> mylist  # see whats inside of our list at the end of the loop
['is', 'blah', 'is']



On Saturday 31 May 2003 13:41, Darren Teo wrote:
> hey just wondering how do u extract every second element delimited by the
> ":" on every line and put them all in a list thanx ....

-- 
Jay Dorsey
jay at jay dorsey dot com






More information about the Python-list mailing list