{"id":7386,"library":"lzstring","title":"lzstring Python Library","description":"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.","status":"active","version":"1.0.4","language":"en","source_language":"en","source_url":"https://github.com/gkovacs/lz-string-python","tags":["compression","decompression","lz-string","data-storage","javascript-interop"],"install":[{"cmd":"pip install lzstring","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"LZString","correct":"from lzstring import LZString"}],"quickstart":{"code":"from lzstring import LZString\n\nlzs = LZString()\noriginal_string = \"Hello World! This is a test string for compression.\"\n\n# Compress to a standard lz-string format\ncompressed_data = lzs.compress(original_string)\nprint(f\"Compressed (raw): {compressed_data[:50]}...\")\ndecompressed_data = lzs.decompress(compressed_data)\nprint(f\"Decompressed (raw): {decompressed_data}\")\nassert original_string == decompressed_data\n\n# Compress to Base64 (common for web transfer)\ncompressed_b64 = lzs.compressToBase64(original_string)\nprint(f\"Compressed (Base64): {compressed_b64[:50]}...\")\ndecompressed_b64 = lzs.decompressFromBase64(compressed_b64)\nprint(f\"Decompressed (Base64): {decompressed_b64}\")\nassert original_string == decompressed_b64","lang":"python","description":"Initialize LZString and compress/decompress a simple string using its default and Base64 methods. Demonstrates basic usage and ensures data integrity."},"warnings":[{"fix":"Always match the compression method variant (e.g., `compressToUTF16`, `compressToBase64`) with its corresponding decompression method (e.g., `decompressFromUTF16`, `decompressFromBase64`).","message":"When 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`.","severity":"gotcha","affected_versions":"All versions"},{"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')`).","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Use the correct camelCase method name, e.g., `lzs.compressToBase64()` instead of `lzs.compress_to_base64()`.","cause":"Attempting to call a method using snake_case naming convention when `lzstring` methods follow camelCase.","error":"AttributeError: 'LZString' object has no attribute 'compress_to_base64'"},{"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`.","cause":"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.","error":"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte"}]}