Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Data from APIs!
You have completed Data from APIs!
Preview
Save the data you've received from the API into a CSV file.
Project Ideas
- Gather weather data from the 5 happiest countries according to the world happiness report
- Determine where the International Space Station is at 10 different times
- Use Marvelβs API to gather data on 5 different comics
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hi again.
In this video,
0:00
we're going to work with taking the data
that we've gathered from our API call and
0:02
saving it into a CSV file
that we're going to create.
0:08
That way you can use the data and
an analysis.
0:12
Let's first look at the example
response in the documentation
0:14
to plan out our CSV Keep scrolling.
0:19
And here we go, example response.
0:28
So to make things a little bit easier,
0:30
I'm only going to pick out a few
things for us to save in the CSV file.
0:32
But please feel free if you want to try
practice saving every single bit of
0:37
information that we receive, go for it.
0:42
I'm gonna pull out just a few things.
0:45
So let's see, let's do weather.
0:46
Main, and this kind of tells us
what the sky and things look like.
0:48
So it would be clear, or cloudy,
or rainy, things like that.
0:53
Then let's do main temp because
that'll be like the main temperature
0:57
with is right now.
1:01
And then let's do wind speed.
1:03
So those three things,
we're gonna get the weather main, so
1:04
kind of what it looks like outside, the
temperature and the wind speed, but please
1:09
feel free to add more if you want to
continue practicing with more information.
1:15
So now that we have
the ability to make requests,
1:21
let's work on saving that data to a CSV.
1:25
Minimize, and
let me pull our console down here,
1:28
okay, so we're going to need to add
import CSV, so that we can work with CSVs.
1:33
Now that we have CSV imported,
1:41
let's work on trying to pick out
those three pieces of information.
1:43
So we know where need to get whether
that was like the clear cloudy
1:46
kind of thing that's gonna
be res.json something.
1:50
The temperature, I'm just gonna call
a temp, also a res.json something.
1:53
And I'm actually gonna save,
since we're repeating this over and
1:58
over again, I'm gonna save
that as its own new variable.
2:03
I'm gonna call it data, and
that's gonna be res.json.
2:07
So now here we can do print data,
2:10
Data, data, okay?
2:17
And then wind speed equals data, so
2:20
obviously data is just going
to give us the whole thing,
2:24
so let's work on digging
down into our information.
2:28
So you can see the first thing
is we are in a dictionary,
2:33
say key, here is its value.
2:38
So weather because we need to get to this
weather item here that we wanna get,
2:40
so I need to do our value, our key of
weather in order to get its value.
2:47
So I'm gonna save, I'm in the console,
let's run a clear and let's run the file.
2:53
And you can see where you have a list,
so in order to get rid of that list,
3:00
it looks like there's just this dictionary
inside so only one thing inside of it.
3:04
We scroll this a little bit.
3:11
So I'm gonna do weather, first item in
our list, save, let's call it again.
3:12
Now we have our dictionary and
we want to get to main, and
3:20
so it looks like it's raining
right now in Shanghai.
3:25
So let's do you main, save,
if I run it, rain, perfect.
3:29
So this is what we need
to get to our weather.
3:35
So I'm gonna copy this, Paste.
3:38
And now let's go back and
let's do the temperature.
3:43
So I'm gonna pull up our information here,
and so
3:46
that was just this section weather,
gave us this value.
3:50
Then there's a key with a value of string.
3:55
Key with a value of a dictionary,
which is where our temperature is.
3:59
So we need to do main.
4:03
So I'm gonna come on here.
4:08
Delete all of this, changes out to main,
4:09
hit Save, run our file, and
4:14
we're inside of this dictionary now.
4:17
If we just need temp
which is another key,
4:22
so temp, save, and we get the value.
4:27
So now I can copy this and paste it.
4:33
That gets us our temperature.
4:36
Now let's look at getting wind speed.
4:40
So same thing, that was our key,
that was our value.
4:44
We have a key and a value.
4:47
Key of wind, and it's a dictionary,
so we need to do wind.
4:48
Scroll back up to here.
4:55
Wind, save, run it.
5:01
You can see we get a dictionary,
we need to get the speed so
5:04
that we can get our value,
5:08
Speed, save, and that gets us our value.
5:14
And if you're like, the numbers have
changed, it's probably because we got
5:21
a new set of information and
the wind has increased just a little bit.
5:26
Comma, paste, okay, so
5:30
now we have all of the data
we are trying to gather.
5:34
So I'm gonna comment that out for us,
because we don't need to use it anymore.
5:41
And now we need to create our CSV.
5:47
So the first thing I'm going to do,
5:49
I'm gonna do a header row at the top
of our CSV, this like the column names.
5:52
So we're gonna put in the cities so
we know what city we're talking about.
5:57
Then we can do the country code
because we also have that information.
6:02
Then we have the weather,
we have the temp,
6:10
And we have the wind speed.
6:17
So we have one, two, three, four,
6:20
five different columns that
will be in the CSV, okay?
6:24
Let's see, can we move this a little bit?
6:32
Not much.
6:33
All right, that's okay.
6:34
All right, so now our data, oops we
already have something that's called data.
6:35
Let's call it our info is going to equal
6:42
our variable city, our variable country,
6:47
our variable weather, our temp variable,
6:53
and our wind speed variable, and
7:00
then we can create our CSV.
7:04
So with open,
we're just gonna call it weather.csv.
7:08
And we're going to write into it,
7:14
save this as f for file.
7:20
Inside of here,
we're going to create our writer
7:25
which is our csv.writer, and we pass in f.
7:29
Then we're going to write the header.
7:34
I'm gonna add this in here as a note.
7:39
So this is writer.writerow,
7:40
and we're gonna pass in the header,
7:46
and then we have write data.
7:52
So we have writer.writerow and
7:58
we're gonna pass in our info.
8:05
Hit Save, and we scroll this up
a little bit, and let's run the file.
8:11
Okay, and
you can see a new file was created here.
8:22
And if I click on it,
8:26
you can see we have our header row
with the names of our columns.
8:28
And then we have information, we have
our city, our country code, the weather,
8:31
the temperature, and
the current wind speed.
8:36
Amazing, we sent a request for
information to the weather API,
8:39
received a response with data, and
then save that data into a CSV.
8:43
Knowing how to use an API to gather
data will give you even more options.
8:49
For instance, I once created
a project using Twitch's API to
8:54
track how often a streamers emotes
were used in their channel.
8:59
I then analyze the data to determine
which emotes users loved and
9:04
which ones were rarely used and
could be replaced.
9:09
In the teacher's notes below,
I've added a few ideas for
9:13
practice using APIs to gather data.
9:16
Give them a try,
practice your skills as much as you can.
9:19
And I'd love to see what you make.
9:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up