Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Read & write Java .properties files

Project description

Project Status: Active - The project has reached a stable, usable state and is being actively developed. https://travis-ci.org/jwodder/javaproperties.svg?branch=master https://coveralls.io/repos/github/jwodder/javaproperties/badge.svg?branch=master MIT License

GitHub | PyPI | Documentation | Issues

javaproperties provides support for reading & writing Java .properties files (both the simple line-oriented format and XML) with a simple API based on the json module β€” though, for recovering Java addicts, it also includes a Properties class intended to match the behavior of Java 8’s java.net.Properties as much as is Pythonically possible.

Previous versions of javaproperties included command-line programs for basic manipulation of .properties files. As of version 0.4.0, these programs have been split off into a separate package, javaproperties-cli.

Installation

Just use pip (You have pip, right?) to install javaproperties and its dependencies:

pip install javaproperties

If you happen to be still stuck using Python 2.6, you will need to make sure that the ordereddict package is installed as well: pip install ordereddict

Examples

Dump some keys & values (output order not guaranteed):

>>> properties = {"key": "value", "host:port": "127.0.0.1:80", "snowman": "β˜ƒ", "goat": "🐐"}
>>> print(javaproperties.dumps(properties))
#Mon Sep 26 14:57:44 EDT 2016
key=value
goat=\ud83d\udc10
host\:port=127.0.0.1\:80
snowman=\u2603

Load some keys & values:

>>> javaproperties.loads('''
... #Mon Sep 26 14:57:44 EDT 2016
... key = value
... goat: \\ud83d\\udc10
... host\\:port=127.0.0.1:80
... #foo = bar
... snowman   β˜ƒ
... ''')
{'goat': '🐐', 'host:port': '127.0.0.1:80', 'key': 'value', 'snowman': 'β˜ƒ'}

Dump some properties to a file and read them back in again:

>>> with open('example.properties', 'w', encoding='latin-1') as fp:
...     javaproperties.dump(properties, fp)
...
>>> with open('example.properties', 'r', encoding='latin-1') as fp:
...     javaproperties.load(fp)
...
{'goat': '🐐', 'host:port': '127.0.0.1:80', 'key': 'value', 'snowman': 'β˜ƒ'}

Sort the properties you’re dumping:

>>> print(javaproperties.dumps(properties, sort_keys=True))
#Mon Sep 26 14:57:44 EDT 2016
goat=\ud83d\udc10
host\:port=127.0.0.1\:80
key=value
snowman=\u2603

Turn off the timestamp:

>>> print(javaproperties.dumps(properties, timestamp=None))
key=value
goat=\ud83d\udc10
host\:port=127.0.0.1\:80
snowman=\u2603

Use your own timestamp (automatically converted to local time):

>>> print(javaproperties.dumps(properties, timestamp=1234567890))
#Fri Feb 13 18:31:30 EST 2009
key=value
goat=\ud83d\udc10
host\:port=127.0.0.1\:80
snowman=\u2603

Dump as XML:

>>> print(javaproperties.dumps_xml(properties))
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key">value</entry>
<entry key="goat">🐐</entry>
<entry key="host:port">127.0.0.1:80</entry>
<entry key="snowman">β˜ƒ</entry>
</properties>

And more!

Project details


Release history Release notifications

This version
History Node

0.4.0

History Node

0.3.0

History Node

0.2.1

History Node

0.2.0

History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
javaproperties-0.4.0-py2.py3-none-any.whl (21.4 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Apr 22, 2017
javaproperties-0.4.0.tar.gz (25.7 kB) Copy SHA256 hash SHA256 Source None Apr 22, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page