Problem stripping line feeds
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Sat Jul 24 16:56:25 EDT 2004
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
More information about the Python-list
mailing list