1
00:00:00,690 --> 00:00:07,470
Hi, in this video, I'll show you how to write a Python script which accesses dots of weather forecast

2
00:00:07,470 --> 00:00:12,450
data from a weather API and puts them in a text file.

3
00:00:12,600 --> 00:00:18,840
So this is our input empty text file, but after we were on the script, we're going to get something

4
00:00:18,840 --> 00:00:19,620
like this.

5
00:00:20,790 --> 00:00:30,930
So full cast data, city data, time, temperature and the sky condition every three hours for five

6
00:00:30,930 --> 00:00:31,410
days.

7
00:00:31,860 --> 00:00:33,300
Let's see how we can do that.

8
00:00:35,280 --> 00:00:40,670
I'm going to start by importing requests and then write a function.

9
00:00:40,680 --> 00:00:41,190
Get.

10
00:00:43,040 --> 00:00:51,890
Whether you know what variables we have here, well, City and also units, which will be matrix by

11
00:00:51,890 --> 00:01:00,800
default for other options, you should go to this page after you have signed up for an API key because

12
00:01:00,800 --> 00:01:02,810
you also need an apology for this.

13
00:01:03,020 --> 00:01:07,760
So sign up on open weather map dot org and give the API key.

14
00:01:08,030 --> 00:01:12,970
And then go to this page and you'll see the parameters you have.

15
00:01:12,980 --> 00:01:17,270
So for units, you can answer standard metric imperial.

16
00:01:18,290 --> 00:01:22,460
So I'm going to use metric by default and also by default.

17
00:01:22,460 --> 00:01:26,540
I'm going to have the API key equal to my API key.

18
00:01:33,240 --> 00:01:37,440
A column and then the next step is to construct the URL.

19
00:01:37,710 --> 00:01:39,270
Usually we use a knife string.

20
00:01:39,600 --> 00:01:43,200
So that would be we can go to the documentation.

21
00:01:43,560 --> 00:01:45,900
This is excellent documentation, by the way.

22
00:01:46,230 --> 00:01:47,400
So I'm going to copy that.

23
00:01:47,410 --> 00:01:47,850
You want to.

24
00:01:51,450 --> 00:01:56,400
That's an example you are out and paste it in here.

25
00:01:59,120 --> 00:02:12,230
Add the HTP colon slash slash part as well, I know from the other examples here that you can also add

26
00:02:14,390 --> 00:02:18,050
and see units equal to.

27
00:02:20,300 --> 00:02:26,090
Units, some metrics will go replace that support.

28
00:02:28,900 --> 00:02:35,710
Now, let's see if we got this correctly forecast Q equals to city, not city name.

29
00:02:36,310 --> 00:02:39,190
So this should reflect your function.

30
00:02:39,190 --> 00:02:46,450
Parameter API key should be API underscore key.

31
00:02:47,500 --> 00:02:51,920
So the scene asks that units should be units.

32
00:02:52,030 --> 00:02:52,570
That's fine.

33
00:02:53,170 --> 00:03:06,200
Next, we create a response object so requests don't get and we pass the URL and let's check the content

34
00:03:06,370 --> 00:03:06,770
content.

35
00:03:06,820 --> 00:03:08,290
That's our Jason.

36
00:03:11,370 --> 00:03:13,470
And I'm just going to return to the content.

37
00:03:17,060 --> 00:03:18,920
Just to see what we have so far.

38
00:03:24,890 --> 00:03:28,160
So, City, let's try Washington

39
00:03:31,970 --> 00:03:32,690
and Rome.

40
00:03:37,190 --> 00:03:38,540
And it seems like we've got.

41
00:03:39,680 --> 00:03:48,290
But Jason does a correctly that's fine, but next, I want to dive into these data to understand them

42
00:03:48,290 --> 00:03:48,620
better.

43
00:03:48,620 --> 00:03:50,510
So I'm going to use the debugger here.

44
00:03:50,750 --> 00:03:59,750
I'm going to place the breakpoints in here so that this part is not executed and the execution freezes

45
00:03:59,750 --> 00:04:06,440
at this point so that I can explore the content dictionary of this here that we got printed out.

46
00:04:07,590 --> 00:04:09,210
For that, I'll go to the debugger.

47
00:04:09,690 --> 00:04:12,120
Start the debugger run, it's.

48
00:04:14,030 --> 00:04:17,390
And we should see the contents variable in here.

49
00:04:24,070 --> 00:04:35,260
It has this list key, which has different dictionaries, so from zero to 39.

50
00:04:35,860 --> 00:04:41,380
This corresponds to data for five days every three hours.

51
00:04:41,530 --> 00:04:48,040
So for one day we have AIDS measurements because one day has 24 hours.

52
00:04:48,430 --> 00:04:51,490
That means 24 times an interval of three.

53
00:04:51,730 --> 00:04:53,890
We have eight observations.

54
00:04:54,190 --> 00:04:58,900
So eight for five days, we have 40, so 40 observations.

55
00:04:58,930 --> 00:05:04,420
These are all the observations that we have, and they are nested in the least key.

56
00:05:06,820 --> 00:05:11,080
So if we expand that, we have, these are the keys.

57
00:05:12,250 --> 00:05:13,270
So that's a dictionary.

58
00:05:13,270 --> 00:05:14,470
It has the main key.

59
00:05:14,860 --> 00:05:17,060
The main key is these other dots.

60
00:05:20,270 --> 00:05:23,240
It has this date DTT.

61
00:05:24,440 --> 00:05:33,620
So let's try to keep that that's open and maybe try to loop over these 40 observations.

62
00:05:34,070 --> 00:05:36,980
So for some in.

63
00:05:39,190 --> 00:05:51,880
Content, so content is the dictionary, but we don't iterate over content that we iterate over contents

64
00:05:52,110 --> 00:05:58,090
of lists list, so that will give us this list of 40.

65
00:05:59,120 --> 00:05:59,760
Dictionaries.

66
00:06:00,680 --> 00:06:02,300
So, again, content.

67
00:06:03,480 --> 00:06:12,960
It's a dictionary, this dictionary has a list key, and the value of this list key is a list.

68
00:06:15,030 --> 00:06:20,370
So that's the list, and this list, as you see, has folded dictionaries.

69
00:06:20,670 --> 00:06:24,870
So one dictionary here writes one dictionary their answer one.

70
00:06:25,200 --> 00:06:28,420
So we're iterating over these dictionaries in here.

71
00:06:29,250 --> 00:06:36,540
So item now will be a dictionary, let's say, for Dixie to be more clear.

72
00:06:37,350 --> 00:06:40,140
Let's call it like that for Dixie in content.

73
00:06:41,400 --> 00:06:43,470
So we're going to get this first dictator.

74
00:06:43,950 --> 00:06:45,660
And what do we do with that?

75
00:06:48,130 --> 00:06:49,300
Well, let's find out.

76
00:06:50,530 --> 00:06:53,200
Well, what do we need to bring out?

77
00:06:53,530 --> 00:07:00,880
We said we want the city name, but we can get the city in other ways and then we want the temperature,

78
00:07:01,510 --> 00:07:04,360
the date and the sky condition.

79
00:07:04,780 --> 00:07:14,050
So the dates, as I so was told in these D.T. underscored the full How do we access that value?

80
00:07:14,260 --> 00:07:16,360
How do we access that for every dictionary?

81
00:07:17,770 --> 00:07:21,310
Well, text is part of that dictionary.

82
00:07:21,320 --> 00:07:31,840
So print dick T T T s t should give us that date.

83
00:07:32,380 --> 00:07:33,940
So I'm going to stop the debugger.

84
00:07:38,890 --> 00:07:40,510
And to run the script.

85
00:07:44,850 --> 00:07:46,650
And see what we we getting the command line,

86
00:07:49,530 --> 00:07:58,620
so I also remove VAT return and run again so that we don't get both the content and the dates.

87
00:07:58,980 --> 00:08:01,890
It's too much theater and these are the dates.

88
00:08:02,430 --> 00:08:05,070
So we were able to extract 40 dates.

89
00:08:05,580 --> 00:08:06,690
Now let's extract.

90
00:08:10,510 --> 00:08:14,890
The temperature again, if I opened the debugger.

91
00:08:21,560 --> 00:08:27,500
First, let me delete that so that we don't have errors in the codes otherwise that this will not execute.

92
00:08:30,750 --> 00:08:42,929
So the content lists and then if you open this dictionary, so we access detective, but if you look

93
00:08:42,929 --> 00:08:43,950
inside main.

94
00:08:45,920 --> 00:08:48,530
You see it as a temp value.

95
00:08:50,990 --> 00:09:00,460
Now, this should have been in Celsius degree, but it's not because I put metrics, it should be metric,

96
00:09:00,500 --> 00:09:12,800
so I did leave the SE and stop it and run again and then make sure that breakpoint is here again.

97
00:09:12,800 --> 00:09:18,740
Under the content variable, remove all the break points and run the debugger.

98
00:09:22,430 --> 00:09:25,130
So content list.

99
00:09:29,560 --> 00:09:31,750
And then we have these dictionaries.

100
00:09:32,170 --> 00:09:38,890
And on the main, we have temperature temp, so one point sixty five.

101
00:09:40,720 --> 00:09:46,160
So we're talking about Dixie again than Maine.

102
00:09:46,690 --> 00:09:49,570
And then from Maine, we get.

103
00:09:51,230 --> 00:09:53,690
Temp, that's one.

104
00:09:54,440 --> 00:09:54,780
Right?

105
00:09:54,800 --> 00:10:04,430
And then lastly, we have to extract the sky condition, which is not in Maine, but it is under weather,

106
00:10:05,270 --> 00:10:07,340
weather and you have description.

107
00:10:07,580 --> 00:10:14,210
So again, dicta and weather and then.

108
00:10:17,200 --> 00:10:17,830
Description.

109
00:10:19,980 --> 00:10:24,570
And actually, actually now weather gives us a list.

110
00:10:25,980 --> 00:10:29,010
So this will give us a list, not a dictionary.

111
00:10:29,400 --> 00:10:32,820
So that list has one dictionary inside.

112
00:10:33,300 --> 00:10:39,930
So therefore, from that list, we want to extract the first item of that list, which is the dictionary.

113
00:10:39,930 --> 00:10:42,690
The entire dictionary is the first item of the list.

114
00:10:44,010 --> 00:10:50,130
And then so we place whether zero, a very strong description out of that.

115
00:10:51,210 --> 00:10:54,240
So stop the debugger now and run the script.

116
00:11:00,200 --> 00:11:04,250
And we see that we got the delta, we expected that date stamp.

117
00:11:05,600 --> 00:11:13,400
The time stamp, the Celsius degrees and the condition of the weather, all of the sky.

118
00:11:14,720 --> 00:11:19,220
Whatever you call it, soon we got those printed out.

119
00:11:19,280 --> 00:11:26,240
But let's make a very small change in the codes so that we write them in our text file in here.

120
00:11:26,660 --> 00:11:33,020
So let me delete what we have so far, and you need to keep the cursor here so that everything is added

121
00:11:33,290 --> 00:11:33,920
under that.

122
00:11:36,060 --> 00:11:40,850
And then we go here and before the loop, we want to open a file.

123
00:11:42,440 --> 00:11:44,420
Delta T t.

124
00:11:45,380 --> 00:11:51,830
So that's the path to our existing file and we open that's in append mill to using an a flag.

125
00:11:52,550 --> 00:11:56,090
S file and then we iterate.

126
00:11:56,210 --> 00:12:02,120
So now we want to get all that's loop and indented.

127
00:12:04,640 --> 00:12:15,110
To be under that with context manager, so as we keep the file, so why we keep the file open, we iterate

128
00:12:15,110 --> 00:12:20,450
and then instead of printing, we say, file that right.

129
00:12:21,620 --> 00:12:25,730
Close the parentheses of the right methods here and there.

130
00:12:26,180 --> 00:12:34,940
And then instead of passing multiple arguments to the right methods, we write that we construct the

131
00:12:34,940 --> 00:12:36,080
string and f string.

132
00:12:36,080 --> 00:12:39,080
So F quotes here as well.

133
00:12:39,230 --> 00:12:45,140
Oh, we have an extra parentheses, so remove that the quotes goes in here.

134
00:12:45,740 --> 00:12:46,370
So that's good.

135
00:12:47,150 --> 00:12:49,340
So that looks fine in parentheses.

136
00:12:49,340 --> 00:12:54,140
F quote at the end, quote parentheses.

137
00:12:56,150 --> 00:13:03,920
Then we place curly brackets to grab these variables.

138
00:13:06,700 --> 00:13:09,070
Like that's I lost one.

139
00:13:11,080 --> 00:13:11,950
And run.

140
00:13:13,540 --> 00:13:15,100
Check that out, that's your seat.

141
00:13:16,120 --> 00:13:21,160
Yeah, it's almost an OK, but we need to split those.

142
00:13:21,940 --> 00:13:23,350
So let's go back to the codes.

143
00:13:24,400 --> 00:13:29,200
And what I'd like to add is at the end before the closing.

144
00:13:31,240 --> 00:13:37,150
Quotes, I want to add a backslash and which will create a break line between the delta.

145
00:13:37,180 --> 00:13:41,730
So I ran again, the scripts go back here and so this is what we get.

146
00:13:43,030 --> 00:13:47,830
So this is appended through the existing data, by the way, anytime you execute it.

147
00:13:48,760 --> 00:13:51,610
New data are appended, as you see.

148
00:13:53,950 --> 00:13:56,980
So let's delete those again.

149
00:14:01,680 --> 00:14:02,850
All of them run.

150
00:14:04,780 --> 00:14:05,980
Data added in here.

151
00:14:07,790 --> 00:14:08,450
Run again.

152
00:14:08,720 --> 00:14:17,420
More dogs are added, so we that's you can schedule with scripts, perhaps to run every 24 hours or

153
00:14:17,420 --> 00:14:26,990
every five days so you can build a continuous does a file containing weather forecasts for a long time.

154
00:14:32,090 --> 00:14:36,290
And that's the final scripts we don't need to return to the contents or we delete that.

155
00:14:37,100 --> 00:14:45,140
So only 11 lines, but quite complex with this nested dictionaries that we've got here.

156
00:14:45,170 --> 00:14:49,250
So you have to be patience when it comes to passing data.

157
00:14:49,370 --> 00:14:54,890
You have to be patient to understand the data first and then know how to apply.

158
00:14:56,220 --> 00:15:05,130
This nest, it's accessing operations, as we did in here, so I thank you a lot for falling and simulating.

