473,508 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string stripping issues

Hello,

I am encountering a behavior I can think of reason for. Sometimes,
when I use the .strip module for strings, it takes away more than what
I've specified. For example:
a = ' <TD WIDTH=175><FONT SIZE=2>Hughes. John</FONT></TD>\r\n' a.strip(' <TD WIDTH=175><FONT SIZE=2>')
returns:

'ughes. John</FONT></TD>\r\n'

However, if I take another string, for example:
b = ' <TD WIDTH=175><FONT SIZE=2>Kim, Dong-Hyun</FONT></TD>\r\n' b.strip(' <TD WIDTH=175><FONT SIZE=2>')


returns:

'Kim, Dong-Hyun</FONT></TD>\r\n'

I don't understand why in one case it eats up the 'H' but in the next
case it leaves the 'K' alone.

Mar 2 '06 #1
7 1504
orangeDinosaur wrote:
I am encountering a behavior I can think of reason for. Sometimes,
when I use the .strip module for strings, it takes away more than what
I've specified. For example:
a = ' <TD WIDTH=175><FONT SIZE=2>Hughes. John</FONT></TD>\r\n' a.strip(' <TD WIDTH=175><FONT SIZE=2>')
returns:

'ughes. John</FONT></TD>\r\n'

However, if I take another string, for example:
b = ' <TD WIDTH=175><FONT SIZE=2>Kim, Dong-Hyun</FONT></TD>\r\n' b.strip(' <TD WIDTH=175><FONT SIZE=2>')
returns:

'Kim, Dong-Hyun</FONT></TD>\r\n'

I don't understand why in one case it eats up the 'H' but in the next
case it leaves the 'K' alone.

That method... I do not think it means what you think it means. The
argument to str.strip is a *set* of characters, e.g.:
foo = 'abababaXabbaXabababbbb'
foo.strip('ab') 'XabbaX' foo.strip('aabababaab') # no difference! 'XabbaX'

For more info, see the string method docs:
http://docs.python.org/lib/string-methods.html
To do what you're trying to do, try this:
prefix = 'hello '
bar = 'hello world!'
if bar.startswith(prefix): bar = bar[:len(prefix)] ... bar

'world!'

--Ben

Mar 2 '06 #2
from the python manual:

strip( [chars])
The chars argument is not a prefix or suffix; rather, all combinations
of its values are stripped:
'www.example.com'.strip('cmowz.') 'example'

in your case since the letter 'H' is in your [chars] and the name
starts with an H it gets stripped, but with the second one the first
letter is a K so it stops there.
Maybe you can use:
a[31:] 'Hughes. John</FONT></TD>\r\n' b[31:] 'Kim, Dong-Hyun</FONT></TD>\r\n'

but maybe what you REALLY want is:
a[31:-14] 'Hughes. John' b[31:-14]

'Kim, Dong-Hyun'

Mar 2 '06 #3
Ben Cartwright wrote:
orangeDinosaur wrote:
I am encountering a behavior I can think of reason for. Sometimes,
when I use the .strip module for strings, it takes away more than what
I've specified. For example:
>> a = ' <TD WIDTH=175><FONT SIZE=2>Hughes. John</FONT></TD>\r\n'

>> a.strip(' <TD WIDTH=175><FONT SIZE=2>')


returns:

'ughes. John</FONT></TD>\r\n'

However, if I take another string, for example:
>> b = ' <TD WIDTH=175><FONT SIZE=2>Kim, Dong-Hyun</FONT></TD>\r\n'

>> b.strip(' <TD WIDTH=175><FONT SIZE=2>')


returns:

'Kim, Dong-Hyun</FONT></TD>\r\n'

I don't understand why in one case it eats up the 'H' but in the next
case it leaves the 'K' alone.

That method... I do not think it means what you think it means. The
argument to str.strip is a *set* of characters, e.g.:
>>> foo = 'abababaXabbaXabababbbb'
>>> foo.strip('ab') 'XabbaX' >>> foo.strip('aabababaab') # no difference! 'XabbaX'

For more info, see the string method docs:
http://docs.python.org/lib/string-methods.html
To do what you're trying to do, try this:
>>> prefix = 'hello '
>>> bar = 'hello world!'
>>> if bar.startswith(prefix): bar = bar[:len(prefix)] ... >>> bar 'world!'

Apologies, that should be:
prefix = 'hello '
bar = 'hello world!'
if bar.startswith(prefix): bar = bar[len(prefix):] ... bar

'world!'

--Ben

Mar 2 '06 #4
thanks!

Mar 2 '06 #5
This seems like a web page parsing question. Another approach can be as
follows if you know the limiting token strings:

a.split(' <TD WIDTH=175><FONT
SIZE=2>')[1].split('</FONT></TD>\r\n')[0]

Mar 3 '06 #6

Ben Cartwright wrote:
Ben Cartwright wrote:
orangeDinosaur wrote:
I am encountering a behavior I can think of reason for. Sometimes,
when I use the .strip module for strings, it takes away more than what
I've specified. For example:

>>> a = ' <TD WIDTH=175><FONT SIZE=2>Hughes. John</FONT></TD>\r\n'

>>> a.strip(' <TD WIDTH=175><FONT SIZE=2>')

returns:

'ughes. John</FONT></TD>\r\n'

However, if I take another string, for example:

>>> b = ' <TD WIDTH=175><FONT SIZE=2>Kim, Dong-Hyun</FONT></TD>\r\n'

>>> b.strip(' <TD WIDTH=175><FONT SIZE=2>')

returns:

'Kim, Dong-Hyun</FONT></TD>\r\n'

I don't understand why in one case it eats up the 'H' but in the next
case it leaves the 'K' alone.

That method... I do not think it means what you think it means. The
argument to str.strip is a *set* of characters, e.g.:
>>> foo = 'abababaXabbaXabababbbb'
>>> foo.strip('ab')

'XabbaX'
>>> foo.strip('aabababaab') # no difference!

'XabbaX'

For more info, see the string method docs:
http://docs.python.org/lib/string-methods.html
To do what you're trying to do, try this:
>>> prefix = 'hello '
>>> bar = 'hello world!'
>>> if bar.startswith(prefix): bar = bar[:len(prefix)]

...
>>> bar

'world!'

Apologies, that should be:
>>> prefix = 'hello '
>>> bar = 'hello world!'
>>> if bar.startswith(prefix): bar = bar[len(prefix):] ... >>> bar

'world!'


or instead of:

a.strip(' <TD WIDTH=175><FONT SIZE=2>')

use:

a.replace(' <TD WIDTH=175><FONT SIZE=2>','')

Iain

Mar 3 '06 #7
orangeDinosaur wrote:
Hello,

I am encountering a behavior I can think of reason for. Sometimes,
when I use the .strip module for strings, it takes away more than what
I've specified. For example:
a = ' <TD WIDTH=175><FONT SIZE=2>Hughes. John</FONT></TD>\r\n' a.strip(' <TD WIDTH=175><FONT SIZE=2>')
returns:

'ughes. John</FONT></TD>\r\n'

However, if I take another string, for example:
b = ' <TD WIDTH=175><FONT SIZE=2>Kim, Dong-Hyun</FONT></TD>\r\n' b.strip(' <TD WIDTH=175><FONT SIZE=2>')


returns:

'Kim, Dong-Hyun</FONT></TD>\r\n'

I don't understand why in one case it eats up the 'H' but in the next
case it leaves the 'K' alone.

Others have explained the exact problem, I'll make a suggestion.
Take a few minutes to look at BeautifulSoup. It parses HTML code
and allows for extractions of data from strings like this in a
very easy to use way. If this is a one-off thing, don't bother.
If you do this commonly, BeautifulSoup is worth a little study.

-Larry Bates
Mar 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1857
by: Stijn Goris | last post by:
hi all, Does a function excist that converts a string to a string thats compatible with linux file system? kind regards Stijn
4
3312
by: joram gemma | last post by:
Hello, on windows python 2.4.1 I have the following problem >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona' >>> print s D:\music\D\Daniel Lanois\For the beauty of Wynona >>>...
4
2089
by: grghoward | last post by:
I am receiving a series of Microsoft Word documents from web clients that they upload to my server. I need to convert them to XML to pass through to another system. I have done this through...
1
1638
by: Sandy Lewanscheck | last post by:
Hey everybody! I have a problem stripping a variable number of <br> and \n in the beginning of a string. The stripping should only be done BEFORE and real text comes. So "<br><br>\n Hello <br>...
18
2394
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
7
4849
by: Raj | last post by:
Hi I was hoping someone could suggest a simple way of stripping non-numeric data from a string of numbers. For example, if I have "ADB12458789\n" I would like to remove the letters and the...
7
3633
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
33
3634
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
7
2275
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.