admin管理员组文章数量:1327929
I am trying to get data from the following CSV which is in my local folder as bids.csv
:
1, 3000
4, 2500
15, 2000
into an array. I'm using the jquery-csv
library but without success.
Here is my entire code:
<html><head>
<script type="text/JavaScript" src=".10.1/jquery.min.js" ></script>
<script src="jquery-csv.js"></script>
</head>
<body>
<script type="text/javascript">
data = $.csv.toArray('bids.csv');
console.log(data)
</script>
</body></html>
When I go to JS console in chrome, there is indeed an array in the console, but it seems to not have my data that I was looking for, instead a huge amount of guff. I can only assume this is the default array that jquery-csv outputs and that it is not reading my csv for some reason.
I am trying to get data from the following CSV which is in my local folder as bids.csv
:
1, 3000
4, 2500
15, 2000
into an array. I'm using the jquery-csv
library but without success.
Here is my entire code:
<html><head>
<script type="text/JavaScript" src="http://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js" ></script>
<script src="jquery-csv.js"></script>
</head>
<body>
<script type="text/javascript">
data = $.csv.toArray('bids.csv');
console.log(data)
</script>
</body></html>
When I go to JS console in chrome, there is indeed an array in the console, but it seems to not have my data that I was looking for, instead a huge amount of guff. I can only assume this is the default array that jquery-csv outputs and that it is not reading my csv for some reason.
Share Improve this question edited Mar 19, 2019 at 15:29 chazsolo 8,5141 gold badge24 silver badges45 bronze badges asked Mar 19, 2019 at 15:19 mcplumsmcplums 2592 gold badges3 silver badges9 bronze badges2 Answers
Reset to default 3This problem happens because you first need to retrieve the CSV file, then only pass it to jQuery-csv
plugin.
As per the plugin documentation:
$.csv.toArray(
csv
);[...]
csv
(required) The csv data to be transformed.
The code shared in your question considers bids.csv
to be the CSV data.
Assuming the file is accessible from a server (localhost works):
One way to retrieve the CSV data is to emit an Ajax GET request, jQuery's has a feature built-in for this: jQuery.get.
The solution would then be:
<html><head>
<script type="text/JavaScript" src="http://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js" ></script>
<script src="jquery-csv.js"></script>
</head>
<body>
<script type="text/javascript">
// sent a GET request to retrieve the CSV file contents
$.get( "bids.csv", function( CSVdata) {
// CSVdata is populated with the file contents...
// ...ready to be converted into an Array
data = $.csv.toArray(CSVdata);
console.log(data)
});
</script>
</body></html>
It is left as an exercise to the reader to manage errors which may happen either during the file retrieval, or during the conversion.
If you don't want to carry the somewhat heavy jQuery library, other libraries you may want to look at are:
- papaParse
- d3-request and d3-dsv
I can see that you added the file name to the array. You need to read the file using FileReader and then use the toArrays() function.
You can see the example here
本文标签: javascriptGet CSV into array using jquerycsvStack Overflow
版权声明:本文标题:javascript - Get CSV into array using jquery-csv - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742254041a2441296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论