Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Convert plaintext to structured data
0:00 Recall now that you've seen the API,
0:01 what we have to do is we have to pass over to the URL:
0:04 city equals city value, country equals country value.
0:09 But that's not what we're getting here.
0:11 We're just getting plain text that looks like "Portland, USA"
0:14 we can't say the city or the country is "Portland, USA",
0:18 It's got to be broken into pieces and understood,
0:21 right? So that's what we're gonna do now.
0:22 We're going to convert this plain text over to data.
0:26 So, let's just put for now,
0:28 location, I'll write that here in a second, and for a second and then we'll improve upon
0:33 it. We're gonna go over here and say "convert_plaintext_location()" and this will be the location text.
0:38 So we gotta go write this function and it's gonna be some cool string manipulation.
0:42 And let's just print out as an F-string
0:44 "the location equals the location", what we got back.
0:51 Alright now, First of all,
0:52 if for some reason we get no location at all,
0:55 we don't want to try to process it.
0:57 We're just going to return nothing.
0:59 So if there's no location specified or it's empty or something like that,
1:06 then we just want to say
1:07 "you know what? There's no location contained in empty text".
1:11 This is false if its both either none or just the string quote like this.
1:16 But it could just be like a space or a tab or a return character
1:21 sometimes it also doesn't mean much, so this strip will take that away.
1:26 And we'll say "If there's not anything here,
1:27 we're gonna, either it's truly nothing or it's effectively nothing,
1:31 we can't process that, so there's no location".
1:33 So now we're in a good place to say things like location text equals first;
1:38 maybe you wanna get the lower case version of this and we also don't want any
1:43 extra spaces there. So we could be very restrictive about what we allow people to
1:48 type up here. We could say "you must type city comma state comma country".
1:54 That gets a little tricky because not every country has a state concept
1:58 That's easy to type that goes over to our API.
2:01 So you could say, Well,
2:02 you just type city and country,
2:05 but then lots of countries have duplicates. Like there's at least three Portland's;
2:08 there's a Portland, Oregon,
2:09 Portland, Tennessee, and Portland,
2:10 Maine. So we're gonna need a little bit of flexibility here.
2:13 We have basically three cases: is it just the city? is it the city
2:17 and the country? or the city,
2:18 state, and country? Now, regardless of which of those we have,
2:22 let's just print out "location_text" and run that real quick here,
2:25 and we'll just return a "location_text"
2:28 back for the moment. If we run this and I put Portland,
2:33 USA, notice we're getting "Portland lower case usa" because we did the lower and
2:38 strip and if there were spaces,
2:39 it would take those way away as well.
2:41 But what we need to do is we need to somehow get Portland and USA,
2:45 and Python has really cool mechanisms to take a string and break it apart.
2:49 So let's say parts is going to be "location_text.split()" split means split into
2:56 parts, and then you give it a character on which you want to split.
2:59 So we'll say comma, and now let's just print out parts here,
3:03 see what we get back. So if I say Portland,
3:06 Oregon, USA like that, look
3:08 now we have a list of three strings:
3:11 Portland, Oregon, and
3:13 USA. And there's comments in here,
3:14 but these commas are not in the string.
3:16 These are just the list saying I have things that are in it,
3:19 right? The list represents itself with those separators.
3:22 There is a space right there and a space right there that we need to deal
3:25 with. But other than that,
3:27 we have the three parts. So here's the deal is, do we have just the
3:31 city, the city and the country,
3:33 or the city, state and country?
3:34 And the way that we're going to figure that out is how many parts do we
3:38 have? So if the length, how many parts are there, equals one,
3:43 well, then it's just the city.
3:45 So we're gonna say "city equals nothing,
3:48 state equals nothing, country equals nothing".
3:51 And then, in this case,
3:52 we're gonna say "city equals parts of zero",
3:57 right? That's how we get the element out of the list,
3:59 we say the first one, zero based.
4:01 And then we say "strip()", because we want to actually remove those spaces.
4:05 Remember, Oregon had a space on the front
4:07 if they typed it and things like that.
4:09 And then we want to say "country equals US",
4:13 two letter abbreviation, and actually,
4:15 we can just put that as a default up there,
4:17 Alright? On the other hand,
4:19 if the length of the parts two, then they've said the city and the country, what
4:24 we have assumed here, so this is gonna be the same,
4:27 but the country is going to be the next one in the list.
4:30 Parts one, if they specified it all the way out
4:34 and it's three, then it's city, state, country is the format that we're expecting.
4:39 And if it's not one, two or three,
4:41 it must be some kind of error,
4:43 so we're going to return "None" again.
4:45 Alright, Now, instead of returning this location text,
4:49 we're gonna deal with that in a second.
4:50 Let me just print this out here and now we can show things like "city equals
4:56 city, country equals country". And this is exactly, I guess I'll put state in
5:02 here, this is exactly the style of format of data that we're going to need
5:07 to pass it over to the API.
5:10 Alright, now let's run it.
5:11 Let's just say Portland, city is Portland, State is empty, country is
5:15 US, perfect. If we say Portland, USA,
5:21 We have, city is not what we're looking for but country is good.
5:25 What did we do wrong here?
5:27 Oh, it's because I didn't I didn't put a comma,
5:29 I just wrote it wrong. Here we go.
5:33 City is Portland, country is USA, and then let's do the full thing:
5:37 Portland, Tennessee, USA let's say. Now we've got city is
5:42 Portland, State is Tennessee, and country is USA, perfect. So we're breaking this apart correctly,
5:47 but now we have this challenge.
5:50 Like you could see we can return one thing, we have to return more than one
5:54 thing. How do we get all this stuff back?
5:55 So we're gonna address that in just a minute.
5:57 But just breaking the data apart
5:59 is already done, we just gotta figure out how to pass it around in our program.