简体   繁体   English

将数据从csv文件放入数组(Javascript)

[英]Put data from a csv file into an array (Javascript)

I need to be able to take data from a CSV file (which has been chosen by the user via <input type="file" id="fileInput"> ) and put it into an array. 我需要能够从CSV文件(用户通过<input type="file" id="fileInput"> )中获取数据并将其放入数组中。 Id rather avoid using jquery as I haven't used it before but any help would be appreciated. 我宁愿避免使用jquery,因为我之前没有使用它,但任何帮助将不胜感激。 Here's my code: 这是我的代码:

<div id="page-wrapper">
  <div>
    Select a text file: 
    <input type="file" id="fileInput">
  </div>
  <pre id="fileDisplayArea"><pre>
</div>
<p id="csvData"></p>
<button onClick="test()">Show Text</button>

<script>
function test (){
  var file = fileInput.files[0];
  var textType = /text.*/;
  var csvType = 'application/vnd.ms-excel';

  if (file.type.match(csvType)) {
    var reader = new FileReader();
    reader.onload = function(e) {
      document.getElementById("csvData").innerHTML=reader.result;
    }

    reader.readAsText(file);  
  } else {
    fileDisplayArea.innerText = "File not supported!";
  }
}
</script>

To process a csv type file, there's two things you need to do: 要处理csv类型文件,您需要做两件事:

  • Split in to lines 分成几行
  • Split in to columns 拆分为列

You can do both by using String.prototype.split . 您可以使用String.prototype.split来完成这两项工作。 This method splits a string into an array. 此方法将字符串拆分为数组。 To split on each new line, use the regex /\\n/ . 要在每个新行上拆分,请使用正则表达式/\\n/ To split each column, use the character that is used in your data (probably , or ; ). 要拆分每列,请使用数据中使用的字符(可能,; )。

Here's an example: 这是一个例子:

 // Create an array of arrays // Remove first line // Split by "," function process(dataString) { var lines = dataString .split(/\\n/) // Convert to one string per line .map(function(lineStr) { return lineStr.split(","); // Convert each line to array (,) }) .slice(1); // Discard header line return JSON.stringify(lines, null, 2); } function test() { var file = fileInput.files[0]; var textType = /text.*/; var csvType = 'text/csv'; if (file.type.match(csvType)) { var reader = new FileReader(); reader.onload = function(e) { document.getElementById("csvData").innerHTML = process(reader.result); } reader.readAsText(file); } else { fileDisplayArea.innerText = "File not supported!"; } } 
 <div id="page-wrapper"> <div> Select a text file: <input type="file" id="fileInput" /> </div> <pre id="fileDisplayArea"></pre> <pre id="csvData"></pre> <button onClick="test()">Show Text</button> </div> 

Store this data snippet as a .csv file to test: 将此数据代码段存储为.csv文件以进行测试:

ONE,TWO,THREE
1,2,3
2,4,6
3,6,9

An additional note: if there is a header row in your data, like in the example above, I'd strongly suggest to not map to arrays, but to objects. 另外需要注意:如果数据中有标题行,如上例所示,我强烈建议不要映射到数组,而是映射到对象。 This makes your code easier to read and use: 这使您的代码更易于阅读和使用:

 // Create an array of objects // Use the first line as keys // Split by "," function process(dataString) { var lines = dataString .split(/\\n/) .map(function(lineStr) { return lineStr.split(","); }); var keys = lines[0]; var objects = lines .slice(1) .map(function(arr) { return arr.reduce(function(obj, val, i) { obj[keys[i]] = val; return obj; }, {}); }); return JSON.stringify(objects, null, 2); } function test() { var file = fileInput.files[0]; var textType = /text.*/; var csvType = 'text/csv'; if (file.type.match(csvType)) { var reader = new FileReader(); reader.onload = function(e) { document.getElementById("csvData").innerHTML = process(reader.result); } reader.readAsText(file); } else { fileDisplayArea.innerText = "File not supported!"; } } 
 <div id="page-wrapper"> <div> Select a text file: <input type="file" id="fileInput" /> </div> <pre id="fileDisplayArea"></pre> <pre id="csvData"></pre> <button onClick="test()">Show Text</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM