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:

The code won't run. You have to fix the nums = explode(???) line to explode the line of CSVs into the array nums for printing.

When done, think what you have: you now have a way of getting at the numbers buried in a comma-separated line from a data file! Go on to the next lesson where we'll use it to do some data science on the CO$_2$ data. Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.