String help

Pierre-Frédéric Caillaud peufeu at free.fr
Wed Jun 23 23:23:24 CEST 2004



# read all lines in a list
lines = list(open(filename))

# remove spaces at beginning and end of lines
# if you don't know map, look in the docs, it's useful
stripped_lines = map( string.strip, lines )

# another way
stripped_lines = [ line.strip() for line in open(filename)]

# maybe these is a stripright method ?

# as I understand, any line ending with "-" should be merged to the  
previous one.
# as you're a newbie we'll use a generator and an iterator just to  
motivate you
# to read about these useful thingies

def mergelines( lines ):
	it = iter(lines)
	currentline = lines.next()
	while 1:
		lastline = currentline
		try:
			currentline = lines.next()
		except StopIteration:
			yield lastline
			return

		if lastline.endswith('-'):
			lastline += currentline
		else:
			yield lastline

# then :	
for line in mergelines( stripped_lines ):
	print line

# or you could do it the old fashioned way :
result = [ stripped_lines[0] ]
for line in stripped_lines[1:]:
	if result[-1].endswith('-'):
		result[-1] += line
	else:
		result.append( line )



Maybe this works, maybe not... try it ?	






On Wed, 23 Jun 2004 21:36:50 +0100, Rigga <Rigga at hasnomail.com> wrote:

> Hi,
>
> I am new to python and am working on a script that parses a file and  
> loads
> the results in to variables for processing.  I am having problems when it
> comes to data in the file that wraps over two lines i.e.
>
> las -
>         lomas
>
> What i want to be able to do is to some how strip all the spaces from it
> so while it is contained as a variable so it equal 'las - lomas'
>
> How do I go about doing this? I have tried it using the
> string.strip(myvariable,"") however that doesnt appear to do anything.
>
> Any help appreciated
>
> Rigga



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Python-list mailing list