lzstring Python Library

1.0.4 · active · verified Thu Apr 16

lzstring is a Python port of the JavaScript lz-string library, providing efficient in-memory compression and decompression of strings. It supports various encoding formats like Base64, UTF16, and raw binary, making it suitable for compact data storage or transfer, especially when interoperating with JavaScript applications. The current version is 1.0.4, and the library is mature and stable with infrequent updates.

Common errors

Warnings

Install

Imports

Quickstart

Initialize LZString and compress/decompress a simple string using its default and Base64 methods. Demonstrates basic usage and ensures data integrity.

from lzstring import LZString

lzs = LZString()
original_string = "Hello World! This is a test string for compression."

# Compress to a standard lz-string format
compressed_data = lzs.compress(original_string)
print(f"Compressed (raw): {compressed_data[:50]}...")
decompressed_data = lzs.decompress(compressed_data)
print(f"Decompressed (raw): {decompressed_data}")
assert original_string == decompressed_data

# Compress to Base64 (common for web transfer)
compressed_b64 = lzs.compressToBase64(original_string)
print(f"Compressed (Base64): {compressed_b64[:50]}...")
decompressed_b64 = lzs.decompressFromBase64(compressed_b64)
print(f"Decompressed (Base64): {decompressed_b64}")
assert original_string == decompressed_b64

view raw JSON →