473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem stripping line feeds

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
3 2201
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
1
by: Frank Esser | last post by:
Hello, I read data from a DB field where a line feed ("\n") is in. I put this data as text to a label web control. In the HTML page the line feeds are shown as real line feeds in the HTML...
3
by: Maik Seyring | last post by:
hello to all of you I have got a problem with the xslt-processor transformiix used by firefox. The challange is to replace an special-character (within a string) with a <br/>-tag. it works...
2
by: tom0550 | last post by:
Around December 2008 our MS Access 2003 application started inserting extra line feeds everytime the RTF edit window was opened and saved. It was working fine up to that time. We are using the MS...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
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,...
0
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...

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.