473,394 Members | 1,746 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,394 software developers and data experts.

Replace every n instances of a string

Hello-

I have a function that returns to me a text representation of Unicode
data, which looks like this:

\u0013\u0021\u003c\u003f\u0044\u001f\u006a\u005a\u 0050\u0015\u0018\u001d\u007e\u006b\u004e\u007d\u00 6a\u006e\u0068\u0042\u0026\u003c\u004f\u0059\u0056 \u002b\u001a\u0077\u0065\u006a\u000a\u0021\u005f\u 0025\u003f\u0025\u0024\u007e\u0020\u0011\u0060\u00 2c\u0037\u0067\u007a\u0074\u0074\u0003\u0003\u000f \u0039\u0018\u0059\u0038\u0029\u0001\u0073\u0034\u 0009\u0069\u005e\u0003\u006e\u000d\u004c\u001d\u00
f\u006e\u001b\u006e\u0063\u000b\u0014\u0071\u007c\ u004e\u006a\u0011\u004a\u001f\u0063\u0016\u003d\u0 020\u0065\u003e\u0043\u0012\u0047\u0026\u0062\u000 4\u0025\u003b\u0005\u004c\u002e\u005a\u0070\u0048

I would like to add carriage returns to this for usability. But I
don't want to add a return after each "\u" I encounter in the text
(regexp comes to mind if I did). I want to add a return after each 12
"\\u"s I encounter in the string.

Any ideas? Do I not want to search for "\\u" but instead just insert
a \n after each 72 characters (equivalent to 12 \uXXXX codes)? Would
this provide better performance? If so, what would be the easiest way
to do that?

Thanks much!
Jul 18 '05 #1
1 2423

"Tom Cross" <th**********@hotmail.com> wrote in message
news:62**************************@posting.google.c om...
I have a function that returns to me a text representation of Unicode data, which looks like this: .... I would like to add carriage returns...after each 12
"\\u"s I encounter in the string.

Any ideas? Do I not want to search for "\\u" but instead just insert a \n after each 72 characters (equivalent to 12 \uXXXX codes)? Would this provide better performance? If so, what would be the easiest way to do that?


Split string into list of 6*n (72) char chunks and join with \n:

#unirep = textrep(unidata) #ie, call your func and store result. for
illustration...
unirep =
r'\u0013\u0021\u003c\u003f\u0044\u001f\u006a\u005a \u0050\u0015\u0018'

blocklen = 6*4 #instead of 6*12 to get multiple lines with short
unirep
unilist = []
for i in range(0, len(unirep), blocklen):
unilist.append(unirep[i:i+blocklen])

unilines = '\n'.join(unilist)
print unilines

\u0013\u0021\u003c\u003f
\u0044\u001f\u006a\u005a
\u0050\u0015\u0018

Consider whether you want to change r'\n' to something else like
spaces for easier viewing. If so, do so on unirep before chop into
blocks and adjust blocklen if replacement is not two chars.

Terry J. Reedy


Terry J. Reedy
Jul 18 '05 #2

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

Similar topics

4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
0
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
17
by: Levidikus | last post by:
Normally, I never have any problems with String.Replace(). However, I found that I need to replace multiple instances of the character "ª" (\xAA) with a # symbol. The input file is a simple one...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.