How to generate java .properties files in python

  • Thread starter Arnaud Delobelle
  • Start date
A

Arnaud Delobelle

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top