简体   繁体   English

读取文本文件到数组

[英]reading a text file to array

I have a text file filestoplot.txt whose structure is like this: 我有一个文本文件filestoplot.txt其结构如下:

file1.csv
file2.csv
file3.csv

and I am trying to read this into an array. 我试图将其读入数组。 I tried this: 我尝试了这个:

d3.csv("./filestoplot.txt",
    function(file){
         files = file.map(function(d) { 
             for (key in d) { fn=d[key]; } 
             return fn; }
         )
    }
);

It looks ok, but I noticed the first line ( file1.csv ) of the inputput file ( filestoplot.txt ) is not being read. 看起来还不错,但是我注意到未读取inputput文件( filestoplot.txt )的第一行( file1.csv )。

What is wrong? 怎么了? Thanks. 谢谢。

I do not know d3 very well. 我不太了解d3 I figured out it is a library, but you need to specify what frameworks/libraries you are using in the question. 我发现它是一个库,但是您需要指定问题中使用的框架/库。

It is my understanding that you have a .txt file and in the file is a list of .csv files (one per line) that you want to read in. To do this use ajax, I do not know d3 so I will use jQuery. 据我了解,您有一个.txt文件,并且在该文件中是要阅读的.csv文件列表(每行一个)。要使用ajax,我不知道d3,所以我将使用jQuery 。

$.ajax({
  url: "./filestoplot.txt",
  success: function(x){
    x = $.trim(x).split("\n"); // Make array from new lines
    /*
      x is now an array that looks like:
      ["file1.csv","file2.csv","file3.csv"]
      do what you want with it here
    */ 
  }
});

If I misinterpreted the question let me know in this answers comments and I will attempt to better answer the question. 如果我对这个问题有误解,请在此答案中告知我,我将尝试更好地回答这个问题。 You need to be as specific as possible when asking a question and make sure to state any frameworks/libraries you may be using. 提出问题时,您需要尽可能具体,并确保陈述您可能使用的任何框架/库。

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

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