String help

R

Rigga

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
 
D

Daniel Yoo

: 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:

###' 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!
 
P

Paul McGuire

Rigga said:
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
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

# 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 ?
 
R

Rigga

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
 
R

RiGGa

Daniel said:
: 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:

###
' 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.
 
R

RiGGa

Daniel said:
: 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:

###
' 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top