Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'escape': 0.04; 'encoded': 0.05; '(same': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:\\ 10': 0.09; 'skip:\\ 20': 0.09; 'subject:files': 0.09; 'subject:java': 0.09; 'subject:python': 0.10; 'def': 0.13; '"""encode': 0.16; '(2.6': 0.16; 'escapes': 0.16; 'key/value': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'this:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'this?': 0.19; 'java': 0.21; "doesn't": 0.22; '8bit%:30': 0.23; 'from:addr:web.de': 0.23; 'string': 0.24; 'subject: .': 0.29; 'unicode': 0.29; 'class': 0.29; 'strings.': 0.30; 'quite': 0.32; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'file.': 0.34; 'something': 0.35; 'subject:How': 0.35; 'file': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'skip:_ 10': 0.37; 'could': 0.37; 'received:org': 0.38; 'some': 0.38; 'non': 0.38; 'characters': 0.39; 'e.g.': 0.39; 'else': 0.39; 'files': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'achieve': 0.61; '8bit%:16': 0.84; 'latin1': 0.84; 'inefficient': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: How to generate java .properties files in python Date: Sun, 04 Dec 2011 00:51:16 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084bf9f.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322956281 news.xs4all.nl 6896 [2001:888:2000:d::a6]:48342 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16599 Arnaud Delobelle wrote: > I need to generate some java .properties files in Python (2.6 / 2.7). > It's a simple format to store key/value pairs e.g. > > blue=bleu > green=vert > red=rouge > > The key/value are unicode strings. The annoying thing is that the > file is encoded in ISO 8859-1, with all non Latin1 characters escaped > in the form \uHHHH (same as how unicode characters are escaped in > Python). > > I thought I could use the "unicode_escape" codec. But it doesn't work > because it escapes Latin1 characters with escape sequences of the form > \xHH, which is not valid in a java .properties file. > > Is there a simple way to achieve this? I could do something like this: > > def encode(u): > """encode a unicode string in .properties format""" > return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c > in u).encode("latin_1") > > but it would be quite inefficient as I have many to generate. >>> class D(dict): ... def __missing__(self, key): ... result = self[key] = u"\\u%04x" % key ... return result ... >>> d = D(enumerate(map(unichr, range(256)))) >>> u"ähnlich üblich nötig ΦΧΨ" u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>> u"ähnlich üblich nötig ΦΧΨ".translate(d) u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>> u"ähnlich üblich nötig ΦΧΨ".translate(d).encode("latin1") '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8'