String2Dict
string2dict is a Python library designed to transform complex strings into Python dictionaries. It is particularly useful when working with text-based outputs from language models (LLMs) that need to be parsed into valid JSON objects or Python dictionaries. This class provides functionality for cleaning, sanitizing, and parsing such text data
efficiently.
Since LLMs often return data with extra characters, formatting inconsistencies, or embedded code markers, it can be challenging to directly parse the output into JSON or dictionaries. String2Dict aims to simplify this process by handling common formatting issues and providing a robust parsing mechanism.
Key Features
- Strips Whitespace: Removes unnecessary leading and trailing whitespace from strings.
- Removes Embedded Markers: Cleans code markers like
jsonor ``` to ensure the string is ready for parsing. - Ensures Valid JSON Braces: Adjusts strings to ensure they start and end with curly braces (
{}). - Supports JSON and Python Parsing: Tries to parse strings using
json.loadsfirst, and falls back toast.literal_evalif needed. - Handles Multiple Dictionaries: Extracts and parses multiple dictionary-like strings from a single input.
Installation & Usage
To use String2Dict:
pip install string2dict
from string2dict import String2Dict
s2d=String2Dict()
llm_output = "```json\n{\"name\": \"ChatGPT\", \"version\": \"4.0\"}\n```"
parsed_dict= s2d.run(llm_output)
print(parsed_dict)
Output:
{'name': 'ChatGPT', 'version': '4.0'}
Example 2: Parsing Multiple Dictionaries from a String
# Input string containing multiple dictionaries
llm_output = """
```json
{"name": "ChatGPT", "version": "4.0"}
{"name": "GPT-3", "version": "3.0"}
```"""
# Extract and convert each dictionary into a list of dictionaries
parsed_dicts = s2d.string_to_dict_list(llm_output)
print(parsed_dicts)
Output:
[
{'name': 'ChatGPT', 'version': '4.0'},
{'name': 'GPT-3', 'version': '3.0'}
]
Methods
1. strip_surrounding_whitespace(string: str) -> str
- Strips leading and trailing whitespace from the input string.
- Args:
string(str) - The input string. - Returns: Stripped string.
2. remove_embedded_markers(string: str) -> str
- Removes embedded markers like
jsonand other code block markers. - Args:
string(str) - The input string. - Returns: Cleaned string.
3. ensure_string_starts_and_ends_with_braces(string: str) -> str
- Ensures the string starts and ends with curly braces (
{}). - Args:
string(str) - The input string. - Returns: Adjusted string.
4. parse_as_json(string: str) -> dict
- Attempts to parse the string as JSON using
json.loads. - Args:
string(str) - The input JSON string. - Returns: Parsed dictionary.
5. parse_with_literal_eval(string: str) -> dict
- Attempts to parse the string using Python's
ast.literal_eval. - Args:
string(str) - The input string. - Returns: Parsed dictionary.
6. run(string: str) -> dict
- Processes a string through all cleaning and parsing steps, returning a parsed dictionary.
- Args:
string(str) - The input string. - Returns: Parsed dictionary or
Noneif parsing fails.
7. string_to_dict_list(string: str) -> list
- Extracts multiple dictionaries from a string and converts each to a Python dictionary.
- Args:
string(str) - The input string containing one or more dictionaries. - Returns: A list of parsed dictionaries, or
Noneif parsing fails.
Logging
The String2Dict class supports logging for easier debugging. Set the debug parameter to True when initializing the class to enable detailed logging.
Error Handling
The class handles parsing errors gracefully:
- If
json.loadsfails, it attempts to useast.literal_eval. - If both methods fail, it logs an error and returns
None.
Metadata
Release files for string2dict 0.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| string2dict-0.0.9.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| string2dict-0.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.4 kB
Release files / string2dict-0.0.9.tar.gz
| Download URL | string2dict-0.0.9.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7481604ac457914266d0f5e48243b08090b35bee6ab849fa5ae8b29d2c702545
|
|
BLAKE2b-256 checksum How to use checksums |
5fce600f986aab3be2dc64d5eeebed58690714f9e28022807be5730682e6a8f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / string2dict-0.0.9-py3-none-any.whl
| Download URL | string2dict-0.0.9-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fafb67359f0050e4457c79d814d34f76b087c28594e1d04b90cb8fbd98bd017e
|
|
BLAKE2b-256 checksum How to use checksums |
64884275733a28a26e1492627bee6c4e93aaa149fb259970e854a7b7094c866e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|