[英]Not able to hardcode file directory for CSV parsing using Papa Parse
I am trying to parse a .csv file that will be in an already known local directory using Papa Parse in Javascript. 我正在尝试使用Javascript中的Papa Parse解析一个.csv文件,该文件将位于一个已知的本地目录中。 If I have a mechanism to ask the user to browse for the file, it works like a charm.
如果我有一种要求用户浏览文件的机制,它的工作原理就像是一种魅力。 But I can't figure out how to get it to automatically access the file at a given location.
但是我不知道如何获取它以在给定位置自动访问文件。 I feel like I am missing something simple, but I'm not sure what.
我觉得自己缺少一些简单的东西,但是我不确定是什么。 My code so far:
到目前为止,我的代码:
<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>
csv data: 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
Use jquery to "get" the file contents and use papaparse to parse the csv string: 使用jquery“获取”文件内容,并使用papaparse解析csv字符串:
//parse the data file
var csvfile = "data/data.csv";
$.get(csvfile, function (data) {
var csvdata = Papa.parse(data);
console.log(csvdata);
});
you need to read the data from file before you use it for parsing.the below code is working for me 您需要先从文件中读取数据,然后才能使用它进行解析。以下代码对我有用
Papa.parse(fs.createReadStream(file), {
delimiter: ",",
header: true,
complete: function(results) {
console.log("Finished:", results.data);
done();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.