Registry / serialization / lzstring

lzstring

JSON →
library1.0.4pypypi✓ verified 85d ago

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.

pip install lzstring
INSTALL
IMPORT
SIG · LZSTRING
L
lzstring
serializationpythonv1.0.4
Install
2.7s avg
Import
47ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.050s · 23.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.7s · import 0.045s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LZString
from lzstring import LZString

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
Debug
Known issues
gotchaWhen interoperating with the JavaScript lz-string library or storing compressed data, ensure you use the correct pair of compression and decompression methods. For example, data compressed with `compressToBase64` must be decompressed with `decompressFromBase64`.
fix
Always match the compression method variant (e.g., `compressToUTF16`, `compressToBase64`) with its corresponding decompression method (e.g., `decompressFromUTF16`, `decompressFromBase64`).
affects: All versions
gotchaThe `lzstring` library expects Python `str` objects (Unicode) as input for compression. While it handles internal UTF-8 encoding, providing raw `bytes` without explicit decoding can lead to `TypeError` or incorrect compression/decompression results.
fix
Ensure all input for compression is a Python `str`. If you have `bytes` data, decode it to a `str` first (e.g., `my_bytes.decode('utf-8')`).
affects: All versions
Errors
Common errors & fixes
AttributeError: 'LZString' object has no attribute 'compress_to_base64'
Attempting to call a method using snake_case naming convention when `lzstring` methods follow camelCase.
fix
Use the correct camelCase method name, e.g., `lzs.compressToBase64()` instead of `lzs.compress_to_base64()`.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte
Attempting to decompress corrupted data, data not compressed by `lzstring`, or passing a byte string that isn't valid UTF-8 to a method expecting a Python `str` after some internal operation.
fix
Verify the integrity and origin of the compressed data. Ensure it was compressed using `lzstring` (or a compatible JavaScript `lz-string`) and that you are using the correct decompression method. If the error occurs on input, ensure your input is a valid Python `str`.
Upgrade
Version history
1.0.4latest on PyPI · released Jun 1, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
Resources
lzstring — pip install lzstring · libregistry