How to directly read file on pavlovia

Hi @Xinzhu-Fang, try the following - it creates a list of JS objects (called result) with the contents of your csv file. Put this in a code component in the Begin Experiment tab in the JS code panel.

result = [];
function successFunction(data) {
    var allRows = data.split('\n'); // split rows at new line
    var headerRows = allRows[0].split(',');
    
    for (var i=1; i<allRows.length; i++) {
        var obj = {};
        var currentLine = allRows[i].split(',');
        for(var j=0;j<headerRows.length;j++){
              obj[headerRows[j]] = currentLine[j];
        }
        result.push(obj);
    }
}

$.ajax({
  url: 'resources/tExp.csv',
  dataType: 'text',
  async: false,
}).done(successFunction);

Using this will mean you do not need to import any external libraries.

1 Like