how to parse csv from github?

后端 未结 2 455
盖世英雄少女心
盖世英雄少女心 2021-01-13 16:44
jQuery.ajax({
    url: \"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed         


        
2条回答
  •  -上瘾入骨i
    2021-01-13 17:34

    Parsing CSV is not always as simple as doing "...".split(','). And the file here is a perfect example of that. Some fields contain a ,, and thus are wrapped in quotes.

    I suggest using Papa Parse which will handle that for you. I've used it many times before, it saved me a lot of headaches!

    $.ajax({
        url: "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/6eae5b65a32b679efacf95a2867648330f83a871/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
        success: function(csv) {
            const output = Papa.parse(csv, {
              header: true, // Convert rows to Objects using headers as properties
            });
            if (output.data) {
              console.log(output.data);
            } else {
              console.log(output.errors);
            }
        },
        error: function(jqXHR, textStatus, errorThrow){
            console.log(textStatus);
        }
    });
    
    

提交回复
热议问题