Parsing CSV
CSV (Comma Separated Values) files are text files, commonly used for storing data. Each line contains a row of data. The different pieces of data in each row are separated by a comma. If you open a CSV file in a spreadsheet application such as Microsoft Excel or OpenOffice, you'll notice that each piece of data will be in its own cell. The file I've used looks like the following, if you open it in a text editor.
How to do it...
The first piece of code should look familiar. We've used the loadStrings()
function in the first recipe of this chapter, to load a text file. We'll use it to load a CSV file here.
String[] textLines; void setup() { textLines = loadStrings("processing-websites.csv"); noLoop(); }
Inside the draw()
function, we'll loop through the lines of text. The split()
function is used to split each line into an array with different pieces of data. We'll use a second for
loop to print these pieces of data to the console.
void draw() { background( 255 ); translate...