String help
RiGGa
rigga at hasnomail.com
Thu Jun 24 14:37:31 EDT 2004
Daniel Yoo wrote:
> Rigga <Rigga at 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
More information about the Python-list
mailing list