简体   繁体   English

如何在我的网站上使用我的 CSV 文件中的数据?

[英]How do i use the data in my CSV file on my website?

I'm trying to get data from a CSV file to display it to my website, by using two values as x and y coordinates, like on a graph, and getting the data in that location.我正在尝试从 CSV 文件中获取数据以将其显示到我的网站,方法是使用两个值作为 x 和 y 坐标,就像在图表上一样,并在该位置获取数据。

This is my HTML code这是我的 HTML 代码

<form>
    Width <input type="text" id="W">
    Height <input type="text" id="H">
    Length <input type="text" id="L">
    <button type="button" onclick="f()">Calculate</button>
    <b id="result"></b>
</form>

This is my CSS code这是我的 CSS 代码

function f()
{

    var L = parseInt(document.querySelector("#L").value);
    var W = parseInt(document.querySelector("#W").value);
    var H = parseInt(document.querySelector("#H").value);

    var A;
    var B;
    var calc;

    // These are the X and Y values that i want to use
    A = 2*(H+2)+W+5;
    B = 2*(H+2)+L+5;

    //calc = this the variable where I would like to store the data from the CSV file

    document.querySelector("#result").innerHTML = calc;
}

an example to the data that i have is like this我拥有的数据的一个例子是这样的

   0 1 2 3 4 5 6 7 8 
   | | | | | | | | |
0--1 2 3 4 5 6 7 8 9 
1--2 3 4 5 6 7 8 9 1
2--3 4 5 6 7 8 9 1 2
3--4 5 6 7 8 9 1 2 3
4--5 6 7 8 9 1 2 3 4
5--6 7 8 9 1 2 3 4 5
6--7 8 9 1 2 3 4 5 6
7--8 9 1 2 3 4 5 6 7
8--9 1 2 3 4 5 6 7 8

Basically what I'm trying to do is load the CSV data into an array-like a 2D array and then using the A and B values get whatever is stored there.基本上我想要做的是将 CSV 数据加载到类似数组的二维数组中,然后使用 A 和 B 值获取存储在那里的任何内容。

you need to load csv file with XMLHttpRequest aka AJAX or as File Blob and then you need to parse it before doing any calculation.您需要使用 XMLHttpRequest aka AJAX 或 File Blob 加载 csv 文件,然后您需要在进行任何计算之前对其进行解析。 That is a lot work.这是很多工作。

Hopefully, some brilliant people worked on a solution you can reuse.希望一些杰出的人研究了一个您可以重复使用的解决方案。 https://www.papaparse.com . https://www.papaparse.com Here you can find how to install this library: https://github.com/mholt/PapaParse#install在这里你可以找到如何安装这个库: https://github.com/mholt/PapaParse#install

If you don't want to host the file somewhere and load it with AJAX then you can add file input and manually submit file and PapaParse can work with File Blob.如果您不想在某处托管文件并使用 AJAX 加载它,那么您可以添加文件输入并手动提交文件,PapaParse 可以使用 File Blob。

Add to your html new input:添加到您的 html 新输入:

<input type="file"
       id="csvFile"
       accept="text/csv">

And then add to your Javascript:然后添加到您的 Javascript:

const fileInput = document.querySelector('#csvFile')

fileInput.addEventListener('change', () => {
  Papa.parse(fileInput.files[0], {
    complete: function(results) {
      console.log(results);

      // Here you can go through results and do your calculation
    }
  });
})

Simplest way would be to paste csv into a multiline string and work it out from there.最简单的方法是将 csv 粘贴到多行字符串中,然后从那里解决。

Not recommended for large csv.不推荐用于大型 csv。

function getByXY(x, y) {
//paste your csv in backticks not quotes 
    var data = `1 2 3 4 5 6 7 8 9 
    1 2 3 4 5 6 7 8 9 
    1 2 3 4 5 6 7 8 9`;

//Split by new line to get row
    let rows = a.split("\n");
// check your delimiter can value  can be separated by comma or space  
    let cols = rows[x].split(",");
  // take it out  
   let value = cols[y];
  // throw it back to caller
  return value;
}

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

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