← All ivy-nodes
IVYXSTUDIO · IVY NODE
C

CSV To String Converter

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

ivyx✓

Reads CSV bytes from a buffer and returns them as an aligned text table. Useful for putting a small table into a message or a report.

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

Inputs

FieldTypeDescription
bufferrequiredNot declaredCSV bytes, typically from csv-from-data-frame.

Outputs

FieldTypeDescription
csv_stringrequiredstringNot declared

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.9

  • aligned-table

    Two rows become a right-aligned text table with no index column.

  • empty-buffer

    An empty buffer has no columns to parse.