[Tutor] Using string.strip()
Roeland Rengelink
r.b.rigilink@chello.nl
Wed, 08 Aug 2001 09:11:19 +0200
Hi Sheila,
For me this works as advertised
message = '''
N
;;
no, cs student
;;
N
;;
no
;;
S
;;
case-sensitive
'''
lines = message.split(";;")
print lines
lines = [line.strip() for line in lines]
print lines
gives:
['\nN\n', '\nno, cs student\n', '\nN\n', '\nno\n', '\nS\n',
'\ncase-sensitive\n']
['N', 'no, cs student', 'N', 'no', 'S', 'case-sensitive']
Maybe you can post a complete non-working example.
Roeland
Sheila King wrote:
>
> OK, I'm having another one of my dumb moments...
>
> As I read the docs for the strip() function for strings, it removes all
> leading and trailing characters that are defined in the
> string.whitespace constant, correct? And I even looked in there and SAW
> the '\n' character listed as a whitespace element.
>
> Now, why isn't this working?
>
> I have a file that contains something like this:
>
> N
> ;;
> no, cs student
> ;;
> N
> ;;
> no
> ;;
> S
> ;;
> case-sensitive
> ;;
> Y
> ;;
> yes
> ;;
> N
> ;;
> no
> ;;
>
> I read it in as a string, let's call it messagebody
> Then I do this:
>
> responses = messagebody.split(';;')
>
> Now, that will leave leading and trailing '\n' characters on the items
> in the responses list, so I try this:
>
> for item in responses:
> item = item.strip()
>
> But when I run it and print out the results, I still have leading and
> trailing '\n' characters on the individual list elements, as though the
> strip() function had no effect.
>
> I've tried a few other things, too, but none of them seem to be working,
> either.
>
> I thought that this was the whole purpose of the string.strip() command?
>
You're right, and it should work. Afraid the bug is in your code
somewhere.
> --
> Sheila King
> http://www.thinkspot.net/sheila/
> http://www.k12groups.org/
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
r.b.rigilink@chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"