Pull data from text file?

I would like to use stored data on text files as the source for note sequences. So rather than having arrays inside a Pseq:

Pbind(\instrument, \mysynthdef, \freq, Pseq([120, 200, 333, 88, 120, 300], inf), \dur, 0.3).play;

I could pull those dynamically from a text file containing ONLY the corresponding values, comma separated:

120, 200, 333, 88, 120, 300

I looked into File and saw File.use expects a function, but i guess putting a Pbind there is no good (or rather I did not know how to do that, supposing that’s possible).

Thank you!

Habe a look at
and the examples there


1 Like

Pretty cool, although I encountered some problems, surely my bad.

  1. I get a nil after each line. Perhaps where carriage returns are? Does it matter if the file was made on Windows/Mac/Linux? I think different OSs deal with new lines differently. Not sure if this is in anyway related to this.
    I thought those arguments (true, true) for skipEmptyLines and skipBlanks would actually help there, but i keep getting those “nil” after each line. Is there a way to make it ignore everything that’s not commas or numbers?

  2. If i get it correctly, it creates a 2-dimensional array. I tried flattening it with .flat which of course removes the square brackets but I still get those nils for each of the brackets (i think?).

Now I’m at:

~my = CSVFileReader.readInterpret("/Users/nix/Desktop/text/aa.txt", true, true).flat.postln;


hi Nixie,
i guess this depends a lot on what exactly is in your file.
if i create it as below (adapted from the help examples), it works as i’d expect

// write a test file:
f = File("aa.txt", "w");
f.write("120, 200, 333, 88, 120, 300");
~my = CSVFileReader.readInterpret("aa.txt", true, true).flat.postln;
-> [ 120, 200, 333, 88, 120, 300 ]
1 Like

Right, cause there are no line breaks there, I believe. As soon as you introduce line breaks, CSVFileReader gives you nils, from what I’ve been testing.

ok, that must be due to how you’re creating the file, or, as you said, your OS (i’m on windows currently)

if i create the file with line breaks like this

// write a test file:
f = File("aa.txt", "w");
120, 200, 333, 88, 120, 300
120, 200, 333, 88, 120, 300
120, 200, 333, 88, 120, 300

i don’t get any nils.

anyway, if it’s just the nils specifically, you can reject them like this:

~my = ~my.reject(_.isNil)

// or one time:
~my = CSVFileReader.readInterpret("aa.txt", true, true).flat.reject(_.isNil).postln;

// edit: or slightly more generally
~my = CSVFileReader.readInterpret("aa.txt", true, true).flat.reject(_.isNumber.not).postln;

// edit2: or slightly more elegantly
~my = CSVFileReader.readInterpret("aa.txt", true, true).flat.select(_.isNumber).postln;