如何解析 http/https 响应并将其导出到 csv 文件

How to parse http/https response and export it to a csv file

我有来自 http/https 响应的逗号分隔数据。我想解析它并将其导出到 csv,以便我可以将此 csv 数据提供给 dygraph.js 以制作图表。

这是我写的 node.js 代码,它命中 url 并获取字符串格式的数据,我正在使用 node.js 请求库来命中 url.

这是我在点击 url 后得到的响应,响应的类型是字符串并以逗号分隔。

var str = '"column1","column2","column3","row1col1","row1col2","row1col3","row2col1","row2col2","row2col3",';

str = str.replace(/"/g, '');    // get a rid of quotes

var arr = str.split(',');       // get array

// remove last item since it is empty, there's nothing after last comma   
arr.splice(arr.length-1, 1);    

// you need to know how many columns there is 
// since rows are not terminated by new line -> \n 
// at least on the picture you posted
// let's assume 3 columns
var columns = 3;

var numberOfRows = arr.length / columns; // get number of rows

var newArr = [];

var col2 = 1; //index of the column we want

//now let's create array of objects for each row with only 2nd column
for (var i = 1; i < numberOfRows; i++) { // skip first line i = 1;

    if ( arr.length % columns === 0) {
        newArr[i-1] = {};
        newArr[i-1][arr[col2]] = arr[(i * columns) + 1];

        //if you want column 3 as well
        //newArr[i-1][arr[col2+1]] = arr[(i * columns) + 2];
    }
}

console.log(newArr);

// [ { column2: 'row1col2', column3: 'row1col3' },
//   { column2: 'row2col2', column3: 'row2col3' } ]


// this is from 
function CSV(array) {
    // Use first element to choose the keys and the order
    var keys = [];
    for (var k in array[0]) keys.push(k);

    // Build header
    var result = keys.join(",") + "\n";

    // Add the rows
    array.forEach(function(obj){
        keys.forEach(function(k, ix){
            if (ix) result += ",";
            result += obj[k];
        });
        result += "\n";
    });

    return result;
}

console.log(CSV(newArr));

/*
column2,column3\n
row1col2,row1col3\n
row2col2,row2col3\n
*/