Problem stripping line feeds

R

Robert Oschler

Hello,

I am using the following function to try and strip both carraige returns and
line feeds, ASCII 13 and 10 respectively, from a string. It doesn't seem to
be working:

x = filter(lambda c: c not in "\012\015", string.strip(x)) # octal version

I also tried:

x = filter(lambda c: c not in "\r\n", string.strip(x)) # escape char.
version

What am I doing wrong?

Thanks
 
R

Roel Schroeven

Robert said:
I am using the following function to try and strip both carraige returns and
line feeds, ASCII 13 and 10 respectively, from a string. It doesn't seem to
be working:

x = filter(lambda c: c not in "\012\015", string.strip(x)) # octal version

I also tried:

x = filter(lambda c: c not in "\r\n", string.strip(x)) # escape char.
version

What am I doing wrong?

I don't know, it seems to work for me:
'foobar'

Exactly what happens when you try it?

You could also take the regular expression approach:
'foobarbaz'
 
B

Byron

Hi Robert,

I don't know if this will help too much, but have you tried the following:

import string
cleanedText = string.strip("String with carriage return goes here.")

Byron
 
D

Dennis Lee Bieber

Hi Robert,

I don't know if this will help too much, but have you tried the following:

import string
cleanedText = string.strip("String with carriage return goes here.")
'Assorted text with\nvarious lines and encoded\rslash-r\nslash-n\n\nand
\r\nslash-r slash-n\r\n\nsets. 'Assorted text with
various lines and encoded
slash-r
slash-n

and
slash-r slash-n

sets. 'Assorted text with various lines and encoded slash-r slash-n and
slash-r slash-n sets. 'Assorted text with various lines and encoded slash-r slash-n and
slash-r slash-n sets. 'Assorted text withvarious lines and encodedslash-rslash-nand slash-r
slash-nsets. 'Assorted text withvarious lines and encodedslash-rslash-nand slash-r
slash-nsets.

In truth, I couldn't figure out what that filter/lambda attempt
attempt was trying to do at all... The results of the lambda didn't seem
to be used for anything -- or am I confused, and the parts of the filter
weren't used for anything.
'Assorted text withvarious lines and encodedslash-rslash-nand slash-r
slash-nsets. '
Well, looks like the superfluous part was the original
"string.strip(x)", just feed it "x"

--
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top