Yaz*_*zra 5 javascript csv papaparse
我试图解析一个.csv文件,该文件将在Javascript中使用Papa Parse在已知的本地目录中.如果我有一个机制要求用户浏览该文件,它就像一个魅力.但我无法弄清楚如何让它自动访问给定位置的文件.我觉得我错过了一些简单的东西,但我不确定是什么.我的代码到目前为止:
<html>
<head>
<title>testing papa parse</title>
</head>
<body>
<script src="js/PapaParse-4.0.7/papaparse.min.js"></script>
<script src="js/jquery-2.1.1.min.js"></script>
<script>
var data;
// var file = evt.target.files[0];
var file = "\\files\\telemetryData.csv";
Papa.parse(file, {
//makes sure the first line is interpreted as a header, not data
header: true,
//allows stuff to be typed as their respective primitive types instead of only strings. turned off in this case
dynamicTyping: false,
//allows you to define the delimiter if not a default comma
delimiter: ",",
//allows you to define a comment line, which would be skipped by the parser
comments: "//",
//turns on fastmode, quicker but assumes no quoted fields is present
fastmode: true,
download: true,
step: function(results){
console.log(results.data);
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
csv数据:
TEAM_ID,MISSION_TIME,ALT_SENSOR,OUTSIDE_TEMP,INSIDE_TEMP,VOLTAGE,FSW_STATE
ubArtemis,0,36,20,20,9,1
ubArtemis,1,45,18,20,9,1
ubArtemis,2,50,16,20,9,1
ubArtemis,3,65,14,19,9,1
ubArtemis,4,79,12,17,8,2
//hello this is a comment
ubArtemis,5,100,10,16,8,3
ubArtemis,6,120,8,15,8,4
ubArtemis,7,145,6,14,8,5
ubArtemis,8,160,4,12,7,6
ubArtemis,9,200,2,10,6,7
Run Code Online (Sandbox Code Playgroud)
使用 jquery“获取”文件内容并使用 papaparse 解析 csv 字符串:
//parse the data file
var csvfile = "data/data.csv";
$.get(csvfile, function (data) {
var csvdata = Papa.parse(data);
console.log(csvdata);
});
Run Code Online (Sandbox Code Playgroud)