Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16604

Re: How to generate java .properties files in python

From Peter Otten <__peter__@web.de>
Subject Re: How to generate java .properties files in python
Followup-To gmane.comp.python.general
Date 2011-12-04 11:22 +0100
Organization None
References <CAJ6cK1aLyX0QSA3K3b+Rc+BPi9xZvbYJaEFk3-ijhdQWcdi9_w@mail.gmail.com> <jbecl8$abv$1@dough.gmane.org> <CAJ6cK1Y2sqqb09b+rFQFMpXCzASKcS=k3=DbfvKHLza33K9X9A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3261.1322994132.27778.python-list@python.org> (permalink)

Followups directed to: gmane.comp.python.general

Show all headers | View raw


Arnaud Delobelle wrote:

> On 3 December 2011 23:51, Peter Otten <__peter__@web.de> wrote:
>> 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'
> 
> A very nice solution - thanks, Peter.

I found another one:

>>> u"äöü ΦΧΨ".encode("latin1", "backslashreplace")
'\xe4\xf6\xfc \\u03a6\\u03a7\\u03a8'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to generate java .properties files in python Peter Otten <__peter__@web.de> - 2011-12-04 11:22 +0100

csiph-web