Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17575
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <arnodel@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.021 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'escape': 0.04; 'subject:string': 0.09; 'def': 0.13; 'intermediate': 0.15; 'map:': 0.16; 'simplest': 0.16; 'subject:unicode': 0.16; 'thanks,': 0.18; 'subject:skip:s 10': 0.18; 'received:209.85.212.46': 0.23; 'received:mail-vw0-f46.google.com': 0.23; 'way?': 0.23; 'defined': 0.24; 'all,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'unicode': 0.29; 'strings,': 0.30; 'subject:number': 0.30; "i've": 0.31; 'quite': 0.32; 'there': 0.33; 'to:addr:python-list': 0.34; 'creates': 0.34; 'received:209.85.212': 0.34; 'regular': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'think': 0.37; 'using': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'according': 0.61; 'worth': 0.61; 'inefficient': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=iE7cbxkQvwB/zE0WKAz5qVC0kxWuZwxHp4ksbvbQWWU=; b=fN+A625Mdv1ufjsASKNq28WymWFRgH2s5C4H8e5CmW7wY+NYl/0okYliOcaHXuZzjz IMuk/hk4V2O5lhh0p4enky4Jtk9E5hrCnto9CXZekKKPKxLkQp/odlcg6Ia6aPu7qDza zDu6LHDtFr502sJYqAKD+n85wyvqmDwEsr5Gs= |
| MIME-Version | 1.0 |
| Date | Tue, 20 Dec 2011 14:02:31 +0000 |
| Subject | Performing a number of substitutions on a unicode string |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3863.1324389753.27778.python-list@python.org> (permalink) |
| Lines | 38 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1324389753 news.xs4all.nl 6882 [2001:888:2000:d::a6]:46460 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17575 |
Show key headers only | View raw
Hi all,
I've got to escape some unicode text according to the following map:
escape_map = {
u'\n': u'\\n',
u'\t': u'\\t',
u'\r': u'\\r',
u'\f': u'\\f',
u'\\': u'\\\\'
}
The simplest solution is to use str.replace:
def escape_text(text):
return text.replace('\\', '\\\\').replace('\n',
'\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f')
But it creates 4 intermediate strings, which is quite inefficient
(I've got 10s of MB's worth of unicode strings to escape)
I can think of another way using regular expressions:
escape_ptn = re.compile(r"[\n\t\f\r\\]")
# escape_map is defined above
def escape_match(m, map=escape_map):
return map[m.group(0)]
def escape_text(text, sub=escape_match):
return escape_ptn.sub(sub, text)
Is there a better way?
Thanks,
--
Arnaud
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Performing a number of substitutions on a unicode string Arnaud Delobelle <arnodel@gmail.com> - 2011-12-20 14:02 +0000
csiph-web