Skip to main content

Class CSVReader

Provides methods for reading and parsing CSV files into dictionaries with typed values.

public static class CSVReader

Inheritance​

object→CSVReader

Methods​

Read<T>(TextAsset)​

Reads and parses CSV data from a TextAsset into a list of dictionaries with typed values.

public static List<Dictionary<string, T>> Read<T>(TextAsset textAsset) where T : IConvertible

Parameters​

NameTypeDescription
textAssetTextAssetThe TextAsset containing the CSV data.

Returns​

List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.

Type Parameters​

NameTypeDescription
TThe type to convert each value to. Must implement IConvertible.

Examples​

var data = CSVReader.Read(myTextAsset);
Debug.Log(data.Count);

Read<T>(string)​

Reads and parses CSV data from a raw CSV string into a list of dictionaries with typed values.

public static List<Dictionary<string, T>> Read<T>(string rawText) where T : IConvertible

Parameters​

NameTypeDescription
rawTextstringThe raw CSV data as a string.

Returns​

List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.

Type Parameters​

NameTypeDescription
TThe type to convert each value to. Must implement IConvertible.

Examples​

string rawCSV = "Name,Score\nAlice,95\nBob,88";
var data = CSVReader.Read(rawCSV);
Debug.Log(data[1]["Name"]); // Outputs: Bob

ReadFromFilePath<T>(string)​

Reads CSV data from a file path and parses it into a list of dictionaries with typed values.

public static List<Dictionary<string, T>> ReadFromFilePath<T>(string filePath) where T : IConvertible

Parameters​

NameTypeDescription
filePathstringThe path to the CSV file.

Returns​

List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.

Type Parameters​

NameTypeDescription
TThe type to convert each value to. Must implement IConvertible.

Examples​

var data = CSVReader.ReadFromFilePath("Assets/Data/myData.csv");
Debug.Log(data[0]["Name"]);

ReadHeaders(TextAsset)​

Reads and parses the CSV headers from a TextAsset.

public static string[] ReadHeaders(TextAsset textAsset)

Parameters​

NameTypeDescription
textAssetTextAssetThe TextAsset containing CSV data.

Returns​

string[] — An array of header strings.

Examples​

var headers = CSVReader.ReadHeaders(myTextAsset);
Debug.Log(string.Join(", ", headers));

ReadHeaders(string)​

Reads and parses the CSV headers from a raw CSV string.

public static string[] ReadHeaders(string rawText)

Parameters​

NameTypeDescription
rawTextstringThe raw CSV data as a string.

Returns​

string[] — An array of header strings.

Examples​

string rawCSV = "Name,Age\nJohn,25";
var headers = CSVReader.ReadHeaders(rawCSV);

ReadHeaders(string[])​

Reads and parses the CSV headers from an array of lines.

public static string[] ReadHeaders(string[] lines)

Parameters​

NameTypeDescription
linesstring[]An array of CSV lines.

Returns​

string[] — An array of header strings.

  • Methods
    • Read<T>(TextAsset)
    • Read<T>(string)
    • ReadFromFilePath<T>(string)
    • ReadHeaders(TextAsset)
    • ReadHeaders(string)
    • ReadHeaders(string[])