Skip to main content

Read & write Java .properties files

Project description

Project Status: Active - The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/javaproperties/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/javaproperties.svg MIT License

GitHub | PyPI | Documentation | Issues | Changelog

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.util.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

javaproperties requires Python 3.6 or higher. Just use pip for Python 3 (You have pip, right?) to install it:

python3 -m pip install javaproperties

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>

New in v0.6.0: Dump Unicode characters as-is instead of escaping them:

>>> print(javaproperties.dumps(properties, ensure_ascii=False))
#Tue Feb 25 19:13:27 EST 2020
key=value
goat=🐐
host\:port=127.0.0.1\:80
snowman=☃

And more!

Project details


Download files

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

Source Distribution

javaproperties-0.8.1.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

javaproperties-0.8.1-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file javaproperties-0.8.1.tar.gz.

File metadata

  • Download URL: javaproperties-0.8.1.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for javaproperties-0.8.1.tar.gz
Algorithm Hash digest
SHA256 9dcba389effe67d3f906bbdcc64b8ef2ee8eac00072406784ea636bb6ba56061
MD5 48895f6a9569ba2ed95ac4d793a1bc28
BLAKE2b-256 6852d7db7b671e2d4596c759fb526864837677c1562462e45f0ba46aef9a28c5

See more details on using hashes here.

File details

Details for the file javaproperties-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: javaproperties-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for javaproperties-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e9b43334d6c1a9bffe34e2ece52588e21a7e099869bdaa481a5c6498774e18e
MD5 9e8613e2e73771df351a92735c64f3b5
BLAKE2b-256 47e8c244dd03cecdebaf8116c93afaa1c72c8d4833f078a5d35e00c3d2c3be64

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page