← All ivy-nodes
IVYXSTUDIO · IVY NODE
C

CSV To String Converter

ivy.node.csvto-string-converter · v0.0.0

ivyx

Converts CSV data from a BytesIO buffer into a human-readable string representation. This node reads CSV data from a buffer (typically from csv-from-data-frame), loads it into a pandas DataFrame, and converts it to a formatted string. Useful for displaying CSV data in logs, reports, or text-based outputs. The output can be used for debugging, logging, or displaying data in text format.

#CSV#BytesIO#pandas#DataFrame#Buffer#Data Conversion#String Representation#File Handling

Inputs

FieldTypeDescription
bufferrequiredobjectBytesIO object containing CSV data. (Node reference)
featuresobjectFeatures
labelsobjectLabels

Outputs

FieldTypeDescription
csv_stringrequiredstringNot declared
featuresobjectFeatures
labelsobjectLabels

Source

python

# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
buffer = inp["buffer"]

# Compute
import pandas as pd
import io

class CSVToStringConverter:
    """
    A class to convert CSV data from a BytesIO buffer into a string representation.
    """

    def __init__(self, buffer: io.BytesIO):
        """
        Initialize the CSVToStringConverter with a buffer.

        :param buffer: A BytesIO object containing CSV data.
        """
        self.buffer = buffer

    def convert_to_string(self) -> str:
        """
        Read the CSV data from the buffer and convert it to a string representation.

        :return: A string representation of the CSV data.
        """
        # Reset buffer position to the start
        self.buffer.seek(0)
        
        # Read CSV data into a DataFrame
        data_frame = pd.read_csv(self.buffer)
        
        # Convert DataFrame to string
        return data_frame.to_string(index=False)

csv_to_string_converter = CSVToStringConverter(buffer)
csv_string = csv_to_string_converter.convert_to_string()

# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["csv_string"] = csv_string

Tests

Requires: python:3.11

  • basic-conversion

    Convert valid CSV buffer to string (should succeed).

    csvstringconversionbasic
  • error-invalid-buffer

    Invalid buffer format should handle error gracefully.

    error-handlinginvalid-input