Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'escape': 0.04; 'encoded': 0.05; '(same': 0.07; 'python': 0.08; '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; 'this:': 0.16; 'this?': 0.19; 'java': 0.21; "doesn't": 0.22; 'string': 0.24; 'all,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'subject: .': 0.29; 'unicode': 0.29; 'strings.': 0.30; 'quite': 0.32; 'there': 0.33; 'to:addr:python-list': 0.34; 'file.': 0.34; 'something': 0.35; 'subject:How': 0.35; 'file': 0.36; 'received:209.85.161': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'non': 0.38; 'characters': 0.39; 'e.g.': 0.39; 'else': 0.39; 'files': 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'achieve': 0.61; 'latin1': 0.84; '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=mys8v5U5t7fwGwjGPixH607BpfcINOMBCHLdn+xxIlM=; b=RiZkBeSMNDWBoGChfRwtjd1AtHRSn6SWKfc8nkLRRmSFtvvVruFxyRCsdphWw6DQFM pKbHxvoT4dey/Y13pPoirBT08TK01192oeFdEdiwxZ76AQ4XUxIBds0Vdrnhl56BcsFb vyPfvZtZHJsx5pmv4Pgt4pEK3VhyJe8H5rm/A= MIME-Version: 1.0 Date: Sat, 3 Dec 2011 21:34:08 +0000 Subject: How to generate java .properties files in python From: Arnaud Delobelle To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322948051 news.xs4all.nl 6877 [2001:888:2000:d::a6]:60589 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16597 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