By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

Problem stripping line feeds

P: n/a
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
--
Robert
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robert Oschler wrote:
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:
import string
x = """foo bar""" x 'foo\nbar' x = filter(lambda c: c not in "\r\n", string.strip(x))
x 'foobar'

Exactly what happens when you try it?

You could also take the regular expression approach:
import re
x = 'foo\nbar\rbaz'
x 'foo\nbar\rbaz' x = re.sub('\n|\r', '', x.strip())
x

'foobarbaz'

--
"Codito ergo sum"
Roel Schroeven
Jul 18 '05 #2

P: n/a
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
---
Robert Oschler wrote:
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

Jul 18 '05 #3

P: n/a
On Sun, 25 Jul 2004 00:11:43 GMT, Byron <De*********@netscape.net>
declaimed the following in comp.lang.python:
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.")

Original isn't clear if the <cr> <lf> are only at the end of the
line, or embedded internally.
astring 'Assorted text with\nvarious lines and encoded\rslash-r\nslash-n\n\nand
\r\nslash-r slash-n\r\n\nsets. ' print astring Assorted text with
various lines and encoded
slash-r
slash-n

and
slash-r slash-n

sets.
bstring = " ".join(" ".join(astring.split("\n")).split("\r"))
bstring 'Assorted text with various lines and encoded slash-r slash-n and
slash-r slash-n sets. ' print bstring Assorted text with various lines and encoded slash-r slash-n and
slash-r slash-n sets.
Does leave doubled " " where both <cr> and <lf> were adjacent,
but the other result looks worse to me:
bstring = "".join("".join(astring.split("\n")).split("\r "))
bstring 'Assorted text withvarious lines and encodedslash-rslash-nand slash-r
slash-nsets. ' print bstring 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.
cstring = filter(lambda c: c not in "\r\n", astring)
cstring '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"

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.