Read CSV GML Script

This script takes a filepath to a CSV and imports it into a DS List of DS Map values. Useful for managing large amounts of data such as calendar events, items, or dialogue.

The file makes use of another script posted previously, Explode.

///read_csv(csv)
///@description Returns a DS List of DS Map values from a CSV file
///@param csv

var csv = argument0;
var map = ds_list_create();

//import csv
file = file_text_open_read(csv);

//get columns
var columns = explode(file_text_read_string(file), ",");
file_text_readln(file);

//get rest of data
while(!file_text_eof(file)){
    var dataMap = ds_map_create();
    var data = explode(file_text_read_string(file), ",");
    
    for(var i = 0; i < ds_list_size(data); i++){
        ds_map_add(dataMap, columns[| i], data[| i]);
    }
    
    ds_list_add(map, dataMap);
    file_text_readln(file);
}

file_text_close(file);

return map;

The final DS List looks like this:

Leave a Reply

Your email address will not be published. Required fields are marked *