weixin_33725515 2018-07-06 08:52 采纳率: 0%
浏览 37

如何搜索文件?

So I want to be able to search a CSV file stored on the internet (google drive or google storage bucket) using a key, the CSV file will store a list of names and email addresses.

I need the function to search the CSV file using the name and find the corresponding email address and then store that email address in a variable so I can use it later on.

CSV File Format:

Project Name:               Email:
example-project-sandbox example@example.com

I have a non working attempt at this however I think this is a bit long winded and can be done a lot simpler.

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "https://storage.googleapis.com/file.csv",
    dataType: "text",
    success: function(data) {
      processData(data);
    }
  });
});

function processData(allText) {
  var allTextLines = allText.split(/
|
/);
  var headers = allTextLines[0].split(',');
  var lines = [];

  for (var i = 1; i < allTextLines.length; i++) {
    var data = allTextLines[i].split(',');

    if (data.length == headers.length) {
      var tarr = [];

      for (var j = 0; j < headers.length; j++) {
        tarr.push(headers[j] + ":" + data[j]);
      }
      lines.push(tarr);
    }
  }
  // alert(lines);
}

Any help is greatly appreciated.

  • 写回答

2条回答 默认 最新

  • weixin_33713350 2018-07-06 09:57
    关注

    Use this library.It will really help parsing the file and save time.

    Papaparse

    It's very easy check this example.

    // Parse CSV string
    var data = Papa.parse(csv);
    
    // Convert back to CSV
    var csv = Papa.unparse(data);
    
    // Parse local CSV file
    Papa.parse(file, {
    complete: function(results) {
        console.log("Email:", results.email);
    }
    });
    

    If helped marked the answer and completed. :D

    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真