user3259040
user3259040

Reputation: 155

reading a text file to array

I have a text file filestoplot.txt whose structure is like this:

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.

What is wrong? Thanks.

Upvotes: 1

Views: 332

Answers (1)

Dustin Poissant
Dustin Poissant

Reputation: 3418

I do not know d3 very well. 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.

$.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.

Upvotes: 1

Related Questions