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


Groups > comp.lang.python > #16597 > unrolled thread

How to generate java .properties files in python

Started byArnaud Delobelle <arnodel@gmail.com>
First post2011-12-03 21:34 +0000
Last post2011-12-03 21:34 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  How to generate java .properties files in python Arnaud Delobelle <arnodel@gmail.com> - 2011-12-03 21:34 +0000

#16597 — How to generate java .properties files in python

FromArnaud Delobelle <arnodel@gmail.com>
Date2011-12-03 21:34 +0000
SubjectHow to generate java .properties files in python
Message-ID<mailman.3255.1322948051.27778.python-list@python.org>
Hi all,

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.

-- 
Arnaud

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web