Change function name from list_of_list_to_json to list_of_list_to_dict

This commit is contained in:
yangdx
2025-05-07 10:52:26 +08:00
parent edb3d6ac11
commit 3146309fde
2 changed files with 26 additions and 8 deletions

View File

@@ -719,7 +719,25 @@ def truncate_list_by_token_size(
return list_data
def list_of_list_to_json(data: list[list[str]]) -> list[dict[str, str]]:
def list_of_list_to_dict(data: list[list[str]]) -> list[dict[str, str]]:
"""Convert a 2D string list (table-like data) into a list of dictionaries.
The first row is treated as header containing field names. Subsequent rows become
dictionary entries where keys come from header and values from row data.
Args:
data: 2D string array where first row contains headers and rest are data rows.
Minimum 2 columns required in data rows (rows with <2 elements are skipped).
Returns:
List of dictionaries where each dict represents a data row with:
- Keys: Header values from first row
- Values: Corresponding row values (empty string if missing)
Example:
Input: [["Name","Age"], ["Alice","23"], ["Bob"]]
Output: [{"Name":"Alice","Age":"23"}, {"Name":"Bob","Age":""}]
"""
if not data or len(data) <= 1:
return []