473,403 Members | 2,359 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,403 software developers and data experts.

Removing the continous newline characters from the pythong string

Hi guys i m trying out newline characters and to clean them up
a\n\n\n\n\n\n\n\n\nsss\n\n\n\n\n\n\n\n\n\n\nvvvv\n \n\n\nvsa\n\n\n\nasf
\n\nafs

hello guys

im trying to replace
\n\n\n\n\n\n\n\n\n with \n

thanks for help

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n also with \n as the browser gives
\r carrriage returns

thanks for any help or pointers

May 1 '07 #1
3 1654
On 1 May 2007 14:30:12 -0700, mobil <mo************@gmail.comwrote:
Hi guys i m trying out newline characters and to clean them up
a\n\n\n\n\n\n\n\n\nsss\n\n\n\n\n\n\n\n\n\n\nvvvv\n \n\n\nvsa\n\n\n\nasf
\n\nafs

hello guys

im trying to replace
\n\n\n\n\n\n\n\n\n with \n

thanks for help

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n also with \n as the browser gives
\r carrriage returns

thanks for any help or pointers
Is this what you were looking for?
>>import re
message = '\n\r\n\r\n\n\nhello there\n\r\n!\n'
regex = re.compile('[\n\r]+')
regex.sub('\n', s)
'\nhello there\n!\n'

Ian
May 1 '07 #2
why not use split:
>>>s = " a\n\n\n\n\n\n\n\n\nsss\n\n\n\n\n\n\n\n\n\n\nvvvv\n \n\n\nvsa\n\n\n\nasf... \n\nafs"
s.split()
['a', 'sss', 'vvvv', 'vsa', 'asf...', 'afs']
May 1 '07 #3
What was I thinking? split() will only work if you have no other
whitespace characters in the string. A regex like "[\n\r]+" is indeed
much more appropriate and robust.

Cheers
-Basilisk96

May 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amit Varia | last post by:
How would you remove newline characters from a string? I tried str_replace("\n", "", $string); But that doesn't work.
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
5
by: alex21 | last post by:
I am trying to write a function for determining the data type of columns in a delimited file. However my function is not detecting a newline and exiting the loop after the end of the first line. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.