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.
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, and the 20 into parts.
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.
Share your code
Show a friend, family member, or teacher what you've done!