Replace every n instances of a string

Terry Reedy tjreedy at udel.edu
Fri Aug 15 19:05:36 EDT 2003


"Tom Cross" <thomasacross at hotmail.com> wrote in message
news:62de87da.0308151325.41ea4622 at posting.google.com...
> 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






More information about the Python-list mailing list