Hi, in this video, I'll show you how to write a Python script which accesses dots of weather forecast
data from a weather API and puts them in a text file.
So this is our input empty text file, but after we were on the script, we're going to get something
like this.
So full cast data, city data, time, temperature and the sky condition every three hours for five
days.
Let's see how we can do that.
I'm going to start by importing requests and then write a function.
Get.
Whether you know what variables we have here, well, City and also units, which will be matrix by
default for other options, you should go to this page after you have signed up for an API key because
you also need an apology for this.
So sign up on open weather map dot org and give the API key.
And then go to this page and you'll see the parameters you have.
So for units, you can answer standard metric imperial.
So I'm going to use metric by default and also by default.
I'm going to have the API key equal to my API key.
A column and then the next step is to construct the URL.
Usually we use a knife string.
So that would be we can go to the documentation.
This is excellent documentation, by the way.
So I'm going to copy that.
You want to.
That's an example you are out and paste it in here.
Add the HTP colon slash slash part as well, I know from the other examples here that you can also add
and see units equal to.
Units, some metrics will go replace that support.
Now, let's see if we got this correctly forecast Q equals to city, not city name.
So this should reflect your function.
Parameter API key should be API underscore key.
So the scene asks that units should be units.
That's fine.
Next, we create a response object so requests don't get and we pass the URL and let's check the content
content.
That's our Jason.
And I'm just going to return to the content.
Just to see what we have so far.
So, City, let's try Washington
and Rome.
And it seems like we've got.
But Jason does a correctly that's fine, but next, I want to dive into these data to understand them
better.
So I'm going to use the debugger here.
I'm going to place the breakpoints in here so that this part is not executed and the execution freezes
at this point so that I can explore the content dictionary of this here that we got printed out.
For that, I'll go to the debugger.
Start the debugger run, it's.
And we should see the contents variable in here.
It has this list key, which has different dictionaries, so from zero to 39.
This corresponds to data for five days every three hours.
So for one day we have AIDS measurements because one day has 24 hours.
That means 24 times an interval of three.
We have eight observations.
So eight for five days, we have 40, so 40 observations.
These are all the observations that we have, and they are nested in the least key.
So if we expand that, we have, these are the keys.
So that's a dictionary.
It has the main key.
The main key is these other dots.
It has this date DTT.
So let's try to keep that that's open and maybe try to loop over these 40 observations.
So for some in.
Content, so content is the dictionary, but we don't iterate over content that we iterate over contents
of lists list, so that will give us this list of 40.
Dictionaries.
So, again, content.
It's a dictionary, this dictionary has a list key, and the value of this list key is a list.
So that's the list, and this list, as you see, has folded dictionaries.
So one dictionary here writes one dictionary their answer one.
So we're iterating over these dictionaries in here.
So item now will be a dictionary, let's say, for Dixie to be more clear.
Let's call it like that for Dixie in content.
So we're going to get this first dictator.
And what do we do with that?
Well, let's find out.
Well, what do we need to bring out?
We said we want the city name, but we can get the city in other ways and then we want the temperature,
the date and the sky condition.
So the dates, as I so was told in these D.T. underscored the full How do we access that value?
How do we access that for every dictionary?
Well, text is part of that dictionary.
So print dick T T T s t should give us that date.
So I'm going to stop the debugger.
And to run the script.
And see what we we getting the command line,
so I also remove VAT return and run again so that we don't get both the content and the dates.
It's too much theater and these are the dates.
So we were able to extract 40 dates.
Now let's extract.
The temperature again, if I opened the debugger.
First, let me delete that so that we don't have errors in the codes otherwise that this will not execute.
So the content lists and then if you open this dictionary, so we access detective, but if you look
inside main.
You see it as a temp value.
Now, this should have been in Celsius degree, but it's not because I put metrics, it should be metric,
so I did leave the SE and stop it and run again and then make sure that breakpoint is here again.
Under the content variable, remove all the break points and run the debugger.
So content list.
And then we have these dictionaries.
And on the main, we have temperature temp, so one point sixty five.
So we're talking about Dixie again than Maine.
And then from Maine, we get.
Temp, that's one.
Right?
And then lastly, we have to extract the sky condition, which is not in Maine, but it is under weather,
weather and you have description.
So again, dicta and weather and then.
Description.
And actually, actually now weather gives us a list.
So this will give us a list, not a dictionary.
So that list has one dictionary inside.
So therefore, from that list, we want to extract the first item of that list, which is the dictionary.
The entire dictionary is the first item of the list.
And then so we place whether zero, a very strong description out of that.
So stop the debugger now and run the script.
And we see that we got the delta, we expected that date stamp.
The time stamp, the Celsius degrees and the condition of the weather, all of the sky.
Whatever you call it, soon we got those printed out.
But let's make a very small change in the codes so that we write them in our text file in here.
So let me delete what we have so far, and you need to keep the cursor here so that everything is added
under that.
And then we go here and before the loop, we want to open a file.
Delta T t.
So that's the path to our existing file and we open that's in append mill to using an a flag.
S file and then we iterate.
So now we want to get all that's loop and indented.
To be under that with context manager, so as we keep the file, so why we keep the file open, we iterate
and then instead of printing, we say, file that right.
Close the parentheses of the right methods here and there.
And then instead of passing multiple arguments to the right methods, we write that we construct the
string and f string.
So F quotes here as well.
Oh, we have an extra parentheses, so remove that the quotes goes in here.
So that's good.
So that looks fine in parentheses.
F quote at the end, quote parentheses.
Then we place curly brackets to grab these variables.
Like that's I lost one.
And run.
Check that out, that's your seat.
Yeah, it's almost an OK, but we need to split those.
So let's go back to the codes.
And what I'd like to add is at the end before the closing.
Quotes, I want to add a backslash and which will create a break line between the delta.
So I ran again, the scripts go back here and so this is what we get.
So this is appended through the existing data, by the way, anytime you execute it.
New data are appended, as you see.
So let's delete those again.
All of them run.
Data added in here.
Run again.
More dogs are added, so we that's you can schedule with scripts, perhaps to run every 24 hours or
every five days so you can build a continuous does a file containing weather forecasts for a long time.
And that's the final scripts we don't need to return to the contents or we delete that.
So only 11 lines, but quite complex with this nested dictionaries that we've got here.
So you have to be patience when it comes to passing data.
You have to be patient to understand the data first and then know how to apply.
This nest, it's accessing operations, as we did in here, so I thank you a lot for falling and simulating.