javaproperties
javaproperties is a Python library that provides comprehensive support for reading and writing Java `.properties` files, encompassing both the simple line-oriented format and XML. It offers a straightforward API inspired by Python's `json` module, alongside a `Properties` class designed to emulate the behavior of Java 8's `java.util.Properties` as closely as possible in Python. The library is actively maintained, with the latest version 0.8.2 released in December 2024, and its release cadence is irregular, with significant gaps between major versions.
Warnings
- breaking Version 0.8.0 dropped support for Python 2.7, 3.4, and 3.5. Users on these Python versions must upgrade to Python 3.8+ (or 3.10+ for 0.8.2).
- breaking In version 0.7.0, the `javaproperties.parse()` function's return type changed from triples of strings to a generator of custom `PropertiesElement` objects. Code relying on the old return structure will break.
- breaking As of version 0.5.0, parsing invalid `\uXXXX` escape sequences will now raise an `InvalidUEscapeError` instead of potentially silently failing or producing incorrect output.
- gotcha The command-line interface (CLI) tools (e.g., `javaproperties`, `json2properties`, `properties2json`) were split into a separate `javaproperties-cli` package as of version 0.4.0.
- gotcha Starting with version 0.8.0, the `Properties` and `PropertiesFile` classes no longer explicitly raise `TypeError` when given non-string keys or values. The library now relies on static type checking to enforce type correctness.
Install
-
pip install javaproperties
Imports
- dumps
from javaproperties import dumps
- loads
from javaproperties import loads
- dump
from javaproperties import dump
- load
from javaproperties import load
- Properties
from javaproperties import Properties
Quickstart
import javaproperties
import io
# Example data to work with
data = {
"key": "value",
"host:port": "127.0.0.1:80",
"snowman": "☃",
"goat": "🐐",
"multiline_value": "line1\\nline2"
}
# 1. Serialize a Python dictionary to a .properties string
print("--- DUMPING TO STRING ---")
properties_string = javaproperties.dumps(data, sort_keys=True, timestamp=None)
print(properties_string)
# 2. Deserialize a .properties string back to a Python dictionary
print("\n--- LOADING FROM STRING ---")
loaded_data_string = javaproperties.loads(properties_string)
print(loaded_data_string)
# 3. Serialize to a file-like object (using StringIO for demonstration)
print("\n--- DUMPING TO/LOADING FROM FILE ---")
with io.StringIO() as fp:
javaproperties.dump(data, fp, sort_keys=True, timestamp=None, encoding='latin-1')
fp.seek(0) # Rewind to beginning to read
file_content = fp.read()
print("File content:\n", file_content)
fp.seek(0) # Rewind again for loading
loaded_data_file = javaproperties.load(fp, encoding='latin-1')
print("Loaded from file:", loaded_data_file)
# 4. Using the Properties class (Java-like interface)
print("\n--- USING PROPERTIES CLASS ---")
props = javaproperties.Properties()
props.update(data)
print(f"Properties object (dict-like): {props}")
print(f"Value for 'snowman': {props.get('snowman')}")
# Storing to a file with Properties class
with io.StringIO() as fp_props:
props.store(fp_props, timestamp=None, encoding='latin-1')
fp_props.seek(0)
print("Properties class output:\n", fp_props.read())