WEBVTT

0
00:00.110 --> 00:02.090
I hope you've given that a good go,

1
00:02.090 --> 00:05.090
and now I'm going to run through the solution with you.

2
00:05.450 --> 00:12.530
Here I've got those three links open and they're all documentation on w3schools for different methods

3
00:12.530 --> 00:13.580
in Python.

4
00:13.610 --> 00:17.090
The first one is a method called readlines().

5
00:17.090 --> 00:18.770
It's really important that you have

6
00:18.770 --> 00:23.390
that 's' because there's also another method called deadline(), the singular form.

7
00:23.390 --> 00:31.510
What readlines() does is it returns all the lines in the file as a list, where each line is an item

8
00:31.510 --> 00:32.800
in the list object.

9
00:32.800 --> 00:40.240
For example, we can run this example, and you can see that inside this demo file readlines().

10
00:40.240 --> 00:48.910
There's several lines in that file, and each line after running this method gets added as an item inside

11
00:48.910 --> 00:51.340
a list like this.

12
00:51.820 --> 00:57.640
Now the next method I showed you is the Python String replace() method.

13
00:57.640 --> 01:04.180
And what this method does is it will replace a specified phrase with another specified phrase.

14
01:04.180 --> 01:10.330
If the starting text was, "I like bananas," and you do txt.replace(), you can replace the part where

15
01:10.330 --> 01:17.050
it says bananas with apples, and when you print, you can see the end outcome changes that text to

16
01:17.050 --> 01:18.240
"I like apples."

17
01:18.570 --> 01:23.490
Now the final method that I showed you is the String strip() method.

18
01:23.490 --> 01:29.730
And what this does is it will remove the spaces at the beginning and at the end of the string.

19
01:29.730 --> 01:35.220
So if our starting text has a lot of white space at the beginning and the end, and then we do txt.strip()

20
01:35.220 --> 01:41.580
it'll get rid of all of that extra space and it ends up with just the word.

21
01:41.610 --> 01:45.480
Now if you have a space in between another word,

22
01:47.520 --> 01:48.720
like this,

23
01:48.720 --> 01:55.110
if we hit Run, then you can see that it keeps the space in between the words, but it gets rid of any

24
01:55.110 --> 01:59.430
spaces on either side, which is really, really handy.

25
01:59.430 --> 02:03.570
And you're going to see hopefully why it's really, really useful.

26
02:03.600 --> 02:10.190
So now that we've reviewed these three methods that we haven't seen before, and normally you would

27
02:10.190 --> 02:14.870
probably find these when you actually get stuck and when you're searching StackOverflow,

28
02:15.260 --> 02:22.010
but now that you're aware of those methods, let's go ahead and create a letter using our starting letter,

29
02:22.010 --> 02:23.360
.docx.

30
02:23.360 --> 02:26.480
So I'm going to go ahead and delete everything in here.

31
02:26.480 --> 02:30.140
And now let's go ahead and tackle this challenge.

32
02:30.140 --> 02:34.970
So the first thing I want to do is to get hold of all of the invited_names,

33
02:34.970 --> 02:37.370
and I want to turn them into a list.

34
02:37.370 --> 02:44.360
Now, I can, of course, rather tediously go and add some square brackets, add some quotation marks

35
02:44.360 --> 02:47.690
around it, and then add it into my main.py,

36
02:47.690 --> 02:49.430
but I don't want to do that.

37
02:49.430 --> 02:52.580
Instead, I'm going to get Python to read the file.

38
02:52.580 --> 02:58.510
So using our classic syntax, with open(), I'm going to open up that file.

39
02:58.630 --> 03:05.050
But in order to open it up, I need to specify a string, which is going to be the path that leads me

40
03:05.050 --> 03:07.540
from here to here.

41
03:07.540 --> 03:10.120
So let's think about how we might get there.

42
03:10.120 --> 03:17.470
Now this Input-Output folder is on the same hierarchical level as our current file, where we're writing

43
03:17.470 --> 03:24.880
our code, if we want to use a relative file path, we can simply say go into the current folder, which

44
03:24.880 --> 03:29.770
is mail-merge-project-start, and then find a folder called Input.

45
03:30.760 --> 03:36.790
Inside this folder called Input, we're going to go to another folder called, Names.

46
03:36.940 --> 03:43.690
And then inside that folder of Names, we're going to get hold of that file called invited_names.txt.

47
03:43.690 --> 03:48.250
And PyCharm is super clever and it helps you out with a lot of typing.

48
03:48.250 --> 03:50.260
So it saves you some time as well.

49
03:50.260 --> 03:53.650
And then I'm going to save it as names_file.

50
03:54.250 --> 03:58.900
Now that I've got this file open I'm going to go ahead and read it,

51
03:58.900 --> 04:01.600
so names_file.read().

52
04:01.600 --> 04:06.370
And once I've read it I'm going to save it to a variable called names,

53
04:06.370 --> 04:08.830
and then I'm going to print out my 'names'.

54
04:08.830 --> 04:17.190
So now if I go ahead and hit Run my main.py, you can see that it prints out all of these names individually,

55
04:17.190 --> 04:21.120
because it's getting hold of everything that's in here and just printing it out.

56
04:21.240 --> 04:26.910
Now, at this point, I would really like these names to be in the format of a list.

57
04:26.910 --> 04:33.530
So if you remember, that's where this readlines() method is going to be really helpful because it returns

58
04:33.530 --> 04:37.580
a list containing each line inside the file as a list item.

59
04:38.360 --> 04:43.730
Now, instead of saying names_file.read, let's replace that with readlines().

60
04:43.850 --> 04:52.070
Now when I hit Run, you can see it prints out names, and names is now magically turned into a list.

61
04:52.070 --> 05:00.590
So now that we have our list of names which we've extracted from our invited_names.txt, then we

62
05:00.590 --> 05:07.580
can go ahead and proceed to the next step, where we're going to replace this placeholder in our starting

63
05:07.580 --> 05:10.520
letter with each of these names.

64
05:10.550 --> 05:15.770
Let's go ahead and create a constant at the top which I'll call PLACEHOLDER,

65
05:15.860 --> 05:21.200
and I'm going to set it as the string which is the square brackets,

66
05:21.200 --> 05:28.570
and then the word name, because this is the string that we want to replace from our starting letter.

67
05:28.780 --> 05:32.410
The next step is to open up our starting letter.

68
05:32.410 --> 05:34.420
So again I'm going to use open(),

69
05:34.420 --> 05:36.610
and then I'm going to specify the path.

70
05:36.610 --> 05:42.070
So from here, I'm going to go into the folder that's at the same level as this file.

71
05:42.070 --> 05:44.410
So using the ./,

72
05:44.410 --> 05:47.310
and then I'm going to go into Input again,

73
05:47.310 --> 05:51.660
and then I'm going to go into, instead of names, I'm going to go to the Letters.

74
05:53.160 --> 05:58.140
And I'm going to get hold of my starting_letter.docx.

75
05:58.140 --> 06:01.320
And I'm going to open this as the letter_file.

76
06:02.010 --> 06:09.600
With the letter_file, I'm simply just going to get hold of the letter_contents by saying letter_file.read()

77
06:09.600 --> 06:10.860
.

78
06:10.860 --> 06:17.010
And this is going to be a normal read(), because I want all of the content inside that letter, and it's

79
06:17.010 --> 06:20.910
now going to be saved as a string inside my letter_contents.

80
06:21.420 --> 06:29.430
The next step is to go through the letter_contents and replace that placeholder with the actual name

81
06:29.430 --> 06:31.140
that we've got in our list.

82
06:31.440 --> 06:36.800
To do that, we'll need the second method that I showed you, which is the replace() method.

83
06:36.800 --> 06:41.120
So we can get hold of the text, call replace(),

84
06:41.120 --> 06:47.660
and then the output of this method will be a new string which has modified this text.

85
06:48.110 --> 06:50.300
In our case, we'll need a loop.

86
06:50.300 --> 06:54.170
So we can say for name in our list of names,

87
06:54.170 --> 07:01.220
let's go through each of the names in that list, and then let's get the letter_contents and replace

88
07:01.220 --> 07:08.090
the PLACEHOLDER, so the old string with a new string, which is going to be the name.

89
07:08.180 --> 07:13.820
And once we've replaced it, then we're going to save it into a new_letter.

90
07:14.930 --> 07:20.870
Now that we have our new_letter, let's go ahead and print it out and see what it looks like.

91
07:23.680 --> 07:27.730
So you can see all the contents of each of the letters being printed,

92
07:27.730 --> 07:31.870
but other than the very last letter, that looks pretty normal,

93
07:31.870 --> 07:36.220
every other letter has a new line after the name.

94
07:36.220 --> 07:40.150
So remember when we printed out each of these names?

95
07:40.240 --> 07:46.690
You can see that after each of the names that they've extracted from this list of invited_names, there's

96
07:46.690 --> 07:54.130
a new line that's being added, and you can see that with this \n. The only one that doesn't

97
07:54.130 --> 07:56.650
have a new line is the very last one.

98
07:56.770 --> 08:00.850
What we need to do is to strip the new line.

99
08:00.850 --> 08:02.530
Does that remind you of something?

100
08:02.530 --> 08:08.920
Well, we have a method that we saw called strip(), which can take away any of the spaces at the beginning

101
08:08.920 --> 08:10.480
and at the end of the string.

102
08:10.480 --> 08:17.340
And then we can end up with an output, which is the string without any of those leading and trailing

103
08:17.340 --> 08:18.150
spaces.

104
08:18.330 --> 08:23.010
We're going to loop through each of the names, and then we're going to take the name,

105
08:23.010 --> 08:26.430
and we're going to call strip on each of those names,

106
08:26.430 --> 08:31.110
and then we can save it to a variable called stripped_name.

107
08:31.920 --> 08:36.270
And now we can use that stripped_name instead of the name.

108
08:36.930 --> 08:47.130
Now, if I go ahead and print out our new_letter, you can see that each of the letters look exactly

109
08:47.130 --> 08:49.020
like how we want them to be.

110
08:49.350 --> 08:53.940
All we need to do now is to write them into a new file.

111
08:54.510 --> 09:02.210
In previous lessons, I mentioned that when you open a file that doesn't exist, Python will actually

112
09:02.210 --> 09:04.250
create that file for you.

113
09:04.430 --> 09:10.340
Again, using our with open(), I'm going to navigate to this ReadyToSend folder.

114
09:10.490 --> 09:15.350
Going from where we are in main.py, we're going to go to the Output folder,

115
09:15.350 --> 09:18.500
and then we're going to go to the ReadyToSend folder.

116
09:18.500 --> 09:24.050
And then inside the ReadyToSend folder, is where we're going to create our new file.

117
09:24.050 --> 09:27.770
So let's think about what we want our file name to look like.

118
09:28.070 --> 09:29.510
In the intro I showed you,

119
09:29.510 --> 09:36.020
this is the format for each of the files letter_for _Aang.docx,

120
09:36.020 --> 09:39.200
and then this part is replaced by each of the names.

121
09:39.200 --> 09:45.320
So let's write that, letter_for_{the name},

122
09:45.890 --> 09:48.680
and then .docx.

123
09:48.740 --> 09:53.800
We can use an f-string to replace this part with the stripped_name.

124
09:53.800 --> 10:00.430
So now we should be creating a new_letter for each of the names in our list of names.

125
10:00.430 --> 10:06.340
And those letters are going to have the file name, letter_for_{the name}.docx.

126
10:06.340 --> 10:12.040
So now let's refer to this file as the completed_letter.

127
10:13.570 --> 10:19.180
What we want to do with this completed_letter, which at the moment is blank, is we want to write to

128
10:19.180 --> 10:19.510
it.

129
10:19.510 --> 10:27.580
So to do that, we have to change the open mode from default, which is "r" for read to "w" for write,

130
10:27.580 --> 10:33.100
and this will allow us to get our completed_letter and write to it.

131
10:33.130 --> 10:34.660
What do we want to write in there?

132
10:34.690 --> 10:38.950
Well, we want to write the new_letter that we've created of course.

133
10:40.230 --> 10:43.170
That's pretty much the end of our code,

134
10:43.170 --> 10:47.580
and all we have to do is to go ahead and hit Run.

135
10:47.820 --> 10:54.270
And once I hit Run, you can see there's a whole bunch of letters that have been generated over here,

136
10:54.270 --> 11:00.420
and if I click on each of them, you can see that the name placeholder has now been replaced,

137
11:00.420 --> 11:04.890
and I'm now ready to go to the printers and print all of these out.

138
11:05.520 --> 11:10.500
So did you manage to figure out how to complete this project?

139
11:10.530 --> 11:16.710
If not, be sure to review the relevant parts of the lessons today so that you really confident with

140
11:16.710 --> 11:19.950
what's going on and you understand all of this code.

141
11:20.580 --> 11:25.680
If you want to take a look at the completed solution, then head over to the Course Resources and you'll

142
11:25.680 --> 11:28.650
find a link to the final project code.