From 16d1ae77ee02d0b0d8942aa27367ce8c2c998ae6 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Mon, 27 Jan 2025 10:15:30 +0100 Subject: [PATCH] fixed csv_string_to_list when data contains null --- lightrag/utils.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lightrag/utils.py b/lightrag/utils.py index 9550f688..9792e251 100644 --- a/lightrag/utils.py +++ b/lightrag/utils.py @@ -253,9 +253,23 @@ def list_of_list_to_csv(data: List[List[str]]) -> str: def csv_string_to_list(csv_string: str) -> List[List[str]]: - output = io.StringIO(csv_string) - reader = csv.reader(output) - return [row for row in reader] + # Clean the string by removing NUL characters + cleaned_string = csv_string.replace('\0', '') + + output = io.StringIO(cleaned_string) + reader = csv.reader( + output, + quoting=csv.QUOTE_ALL, # Match the writer configuration + escapechar='\\', # Use backslash as escape character + quotechar='"', # Use double quotes + ) + + try: + return [row for row in reader] + except csv.Error as e: + raise ValueError(f"Failed to parse CSV string: {str(e)}") + finally: + output.close() def save_data_to_file(data, file_name):