Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16604
| 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!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'thanks,': 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; "skip:' 10": 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; '...': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'another': 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; '2011': 0.61; 'achieve': 0.61; '8bit%:16': 0.84; 'latin1': 0.84; 'otten': 0.84; 'peter.': 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 |
| Followup-To | gmane.comp.python.general |
| Date | Sun, 04 Dec 2011 11:22:10 +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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Gmane-NNTP-Posting-Host | p5084b588.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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3261.1322994132.27778.python-list@python.org> (permalink) |
| Lines | 51 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1322994133 news.xs4all.nl 6953 [2001:888:2000:d::a6]:33947 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16604 |
Followups directed to: gmane.comp.python.general
Show key headers only | 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
Re: How to generate java .properties files in python Peter Otten <__peter__@web.de> - 2011-12-04 11:22 +0100
csiph-web