adding items from a text file to a list

Peter Wang pzw1 at cor-no-spam-nell.edu
Wed Oct 31 01:36:42 CET 2001


split()[0] won't work because some items (like "pickled eggs") have
spaces in them.

assuming that you've read the entire file into a list named "rawlist":

----------------------

def myStrip(line):
	index = string.find(line, "(")
	if index != -1:
		return string.strip(line[:index])
	else:
		return line

newlist = map(myStrip, rawlist)

----------------------


-peter


On Tue, 30 Oct 2001 16:14:47 -0800, "Emile van Sebille"
<emile at fenx.com> wrote:

>
>"toflat" <toflatpy2 at oaktown.org> wrote in message
>news:48dbc3f6.0110301612.e0f863c at posting.google.com...
>> 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!
>>
>
>How about split()[0]?
>
>HTH,




More information about the Python-list mailing list