Lesson goal: Data science: Dealing with a line of CSVs

Previous: Dealing with two column data files | Home | Next: Graph a CSV file of CO2 concentration

If you've made it through last lesson, you know that we have to do something about a string that contains two numbers that are separated by a comma (CSVs=comma separated values). To handle this, you have to do a bit of string processing.

In this case, we'll use a function called explode. It does this:
  • Takes in a string that you provide.
  • Takes in a separator character that you provide.
  • Looks for the separator character in the string and...
  • ..."explodes" the string into the parts that exist around the separator.

Investigate the definition of explode by hovering over it below.
Move the mouse over a dotted box for more information.

To "warm up," try out this code:

See how the 10 and the 20 in the string s were exploded around the comma, and put into the variable parts, which is an array. The 10 got exploded into parts[1], and the 20 into parts[2].

We can use explode to process those CSVs coming in from a data file.

Now you try. Fix the explode function and if you can print the "exploded" contents of co2.csv to the screen.

Type your code here:

See your results here: