469,916 Members | 2,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

String help

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
Jul 18 '05 #1
6 1300
Rigga <Ri***@hasnomail.com> wrote:
: 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'

Hi Rigga,

Are you stripping as you're reading the file, line by line, or are you
calling strip() at the very end? Without seeing code, it's a little
hard to tell what you're doing.
: I have tried it using the string.strip(myvariable,"") however that
: doesnt appear to do anything.

Ah, ok. string.strip() is deprecated, so you probably shouldn't use
it.

(... And, also, it is being misused. The delimiter -- the second
argument to string.strip() --- has to be nonempty to have any real
effect.)
Instead, you can just use the strip() method of strings. For example:

###
msg = " hello world "
msg.strip() 'hello world' msg.lstrip() 'hello world ' msg.rstrip()

' hello world'
###
See:

http://www.python.org/doc/lib/string-methods.html

for a comprehensive list of the methods that a string can support.
Good luck!
Jul 18 '05 #2
"Rigga" <Ri***@hasnomail.com> wrote in message
news:pa****************************@hasnomail.com. ..
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

Try combining split() and join(), something like:

testdata = """
this is
some data
that spans
multiple -
lines

"""

print " ".join( testdata.split() )

gives:

this is some data that spans multiple - lines

This even handles tabs, and removes trailing whitespace.
-- Paul

Jul 18 '05 #3


# 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 <Ri***@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/
Jul 18 '05 #4
On Wed, 23 Jun 2004 21:36:50 +0100, Rigga 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

Resolved the problem about 1 min after posting - please ignore this thread
Jul 18 '05 #5
Daniel Yoo wrote:
Rigga <Ri***@hasnomail.com> wrote:
: 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'

Hi Rigga,

Are you stripping as you're reading the file, line by line, or are you
calling strip() at the very end? Without seeing code, it's a little
hard to tell what you're doing.
: I have tried it using the string.strip(myvariable,"") however that
: doesnt appear to do anything.

Ah, ok. string.strip() is deprecated, so you probably shouldn't use
it.

(... And, also, it is being misused. The delimiter -- the second
argument to string.strip() --- has to be nonempty to have any real
effect.)
Instead, you can just use the strip() method of strings. For example:

###
msg = " hello world "
msg.strip() 'hello world' msg.lstrip() 'hello world ' msg.rstrip()

' hello world'
###
See:

http://www.python.org/doc/lib/string-methods.html

for a comprehensive list of the methods that a string can support.
Good luck!

Once again I am helped by this group, thanks its appreciated.

Jul 18 '05 #6
Daniel Yoo wrote:
Rigga <Ri***@hasnomail.com> wrote:
: 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'

Hi Rigga,

Are you stripping as you're reading the file, line by line, or are you
calling strip() at the very end? Without seeing code, it's a little
hard to tell what you're doing.
: I have tried it using the string.strip(myvariable,"") however that
: doesnt appear to do anything.

Ah, ok. string.strip() is deprecated, so you probably shouldn't use
it.

(... And, also, it is being misused. The delimiter -- the second
argument to string.strip() --- has to be nonempty to have any real
effect.)
Instead, you can just use the strip() method of strings. For example:

###
msg = " hello world "
msg.strip() 'hello world' msg.lstrip() 'hello world ' msg.rstrip()

' hello world'
###
See:

http://www.python.org/doc/lib/string-methods.html

for a comprehensive list of the methods that a string can support.
Good luck!

Heres what I did..

As you are aware my variable contained data split over 2 lines as shown
below:

myvariable = "las -
lomas"

I then did the following to 'clean' it up:

splitvariable = string.split(myvariable) # yields ("las" "-" "lomas")
cleanvariable = string.join(splitvariable, " ") # yields "las - lomas"

Hope this helps others. Not sure if its the correct or cleanest way to do it
but hey it worked!

Thanks

Rigga
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Helmut Jarausch | last post: by
5 posts views Thread by Sia Jai Sung | last post: by
6 posts views Thread by Calros Lo | last post: by
5 posts views Thread by Joe Nova | last post: by
8 posts views Thread by Lucky | last post: by
37 posts views Thread by xyz | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.