1
00:00:05,200 --> 00:00:11,840
In the last video, we successfully parsed the data 
in our country_info text file. We read the file, 

2
00:00:11,840 --> 00:00:17,280
and created lists for each country, containing 
the seven pieces of information for each one. 

3
00:00:17,280 --> 00:00:20,960
In this video, we'll put that 
data into a more useable format. 

4
00:00:20,960 --> 00:00:24,800
When you're doing something like this, 
you'll usually have some goal in mind. 

5
00:00:24,800 --> 00:00:29,600
Our goal, for this example, will be to find 
information on any country, using either the 

6
00:00:29,600 --> 00:00:35,520
name of the country, or its two-letter ISO code.
These two and three-letter codes, by the way, 

7
00:00:35,520 --> 00:00:41,840
are defined by the International Standards 
Organisation, in ISO-3166. I'll put a link to 

8
00:00:41,840 --> 00:00:47,280
that standard, in the resources for this video.
Before we create the dictionary that we'll use, 

9
00:00:47,280 --> 00:00:50,325
it would be nice to put all 
these fields into variables. 

10
00:00:50,400 --> 00:00:54,080
That's easy to do, we can unpack 
the list for each country. 

11
00:00:54,080 --> 00:00:57,745
There are seven fields, so 
we'll use seven variables. 

12
00:01:11,440 --> 00:01:15,000
This is why it's essential that 
every line in the file contains seven fields – 

13
00:01:15,000 --> 00:01:19,200
even if some fields are blank.
If a list had fewer than seven items, 

14
00:01:19,200 --> 00:01:23,200
our code would crash when we tried 
to unpack it into seven variables. 

15
00:01:23,200 --> 00:01:27,680
Ok, next we'll print the variables, 
just to make sure everything's working. 

16
00:01:27,680 --> 00:01:30,172
I'll replace the print on line 7:

17
00:01:51,844 --> 00:01:55,529
I've used a newline followed
by a tab, for the separator.

18
00:01:55,529 --> 00:01:59,665
That should make the output a bit 
clearer, as you'll see when we run the program:

19
00:02:01,040 --> 00:02:04,240
Scroll up through the output, and 
the country name appears first, 

20
00:02:04,240 --> 00:02:08,160
with the details about each 
country indented, on a new line. 

21
00:02:08,160 --> 00:02:11,769
Scroll until you can see the 
details for "Antarctica". 

22
00:02:13,680 --> 00:02:17,923
Four of the fields are empty, but we 
still had to parse them out of the data. 

23
00:02:17,923 --> 00:02:21,125
That's why we had the separators, 
with nothing between them. 

24
00:02:21,520 --> 00:02:26,256
Right at the top of the output, the first row in 
the file contained the headings for the fields. 

25
00:02:26,320 --> 00:02:30,617
That can be useful, but we don't want to 
include those entries in our dictionary. 

26
00:02:30,617 --> 00:02:35,120
The simplest way to exclude them, is to read 
that first line, before we iterate over the 

27
00:02:35,120 --> 00:02:40,046
remainder of the file. I'll add a call 
to the readline method, before the loop: 

28
00:02:44,960 --> 00:02:49,025
I'd better explain what's happening 
there, because it might not be obvious. 

29
00:02:49,025 --> 00:02:54,578
Our country_file object is iterable.
Each time you use the readline method to read a line, 

30
00:02:54,578 --> 00:03:00,556
the iterator automatically moves on to the 
next item. That's the next line, in this case. 

31
00:03:00,556 --> 00:03:05,680
So our for loop, on line 5, starts with the second 
line of the file – because we've already read the 

32
00:03:05,680 --> 00:03:09,944
first line, on line 4.
Run the program again, 

33
00:03:11,280 --> 00:03:14,274
And scroll back up to the top of the output again. 

34
00:03:16,160 --> 00:03:20,480
The first entry is now "Afghanistan", 
and we've skipped the field headings. 

35
00:03:20,480 --> 00:03:24,914
Alright, on with the code.
We've got the fields in variables, 

36
00:03:24,914 --> 00:03:29,440
and we can use them to create a dictionary.
We don't need line 8, now that we know the code 

37
00:03:29,440 --> 00:03:33,144
is working, so I'll comment that line out first:

38
00:04:27,140 --> 00:04:29,600
I don't think I need to explain that code.

39
00:04:29,600 --> 00:04:34,240
We create a dictionary, using the values 
that we unpacked from the line of text. 

40
00:04:34,240 --> 00:04:39,085
Run the program, to check that the 
dictionaries are what we expect. 

41
00:04:41,767 --> 00:04:45,120
That's looking good.
Now what do we do with them? 

42
00:04:45,120 --> 00:04:49,600
Each of these dictionaries will let us get a 
particular piece of information for a country. 

43
00:04:49,600 --> 00:04:52,640
What we need now, is some 
way to choose the country. 

44
00:04:52,640 --> 00:04:56,640
We'll start by choosing a country by name. 
That means we need to put each of these 

45
00:04:56,640 --> 00:05:00,640
dictionaries in another dictionary, 
with the key being the country name. 

46
00:05:00,640 --> 00:05:04,792
I'll define the dictionary at the start 
of the code, before we open the file: 

47
00:05:09,360 --> 00:05:14,078
We've now got an empty dictionary, and 
we can add each entry, inside the loop: 

48
00:05:32,800 --> 00:05:36,720
We should end up with a dictionary where each of 
the keys are the name of a country, and the values 

49
00:05:36,720 --> 00:05:41,196
are the dictionary for that country.
We'll have a dictionary containing dictionaries – 

50
00:05:41,196 --> 00:05:44,871
which we've seen before.
Run the program 

51
00:05:46,320 --> 00:05:49,520
and check the last line of 
the output. It's quite long, 

52
00:05:49,520 --> 00:05:53,351
so you'll need to scroll sideways 
to get a feel for the contents. 

53
00:05:57,280 --> 00:06:02,640
Ok, it's time for a challenge.
This challenge is intended to make 

54
00:06:02,640 --> 00:06:06,640
sure you understand the data that 
we've created, from our text file. 

55
00:06:06,640 --> 00:06:10,720
The best way to understand it is to work 
with it, so that's what you'll be doing. 

56
00:06:10,720 --> 00:06:13,680
The challenge is to get a country 
name from the user, then display 

57
00:06:13,680 --> 00:06:18,560
the capital city for the chosen country.
If you struggle, you may want to review the videos 

58
00:06:18,560 --> 00:06:28,960
about dictionaries, in the previous section.
I'll go over my solution, in the next video.

