Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Convert plaintext to structured data

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


Talk Python's Mastodon Michael Kennedy's Mastodon