All right. 1
So in this lesson we're going to practice getting an email body from a single text file with a known 2
structure. 3
I'm going to go ahead and copy this cell right here and then I'm going to paste it down below. 4
Our goal is modifying the code in the cell, 5
so we just print out the message body instead of the header and the message body. 6
Here's what I'm going to do. 7
I'm going to delete this line, as well as this line, as well as this line. 8
And I'm going to add some code between opening the file and closing the file. 9
The first thing I'm going to do is I'm going to create a boolean variable and I'm a call that "is_ 10
body". 11
And then I'm going to set that to False to begin with. Then I'll also create another variable - a list called 12
"lines". 13
So lines is equal to an empty list. 14
So that has the square bracket notation. 15
Now what I'm gonna do is I'm going to read all the lines in our file so I'm going to use a loop and I'm going 16
to loop through our stream, so I'll say "for line in stream:" and then inside the loop I'm 17
going to check if I'm inside the email body. 18
So "if is_body:", "lines.append(line)". What this line of code does is it will 19
take our list - lines; and append a single line from our text file to the list if we are inside the email 20
body. 21
Now the natural question is: How would you know that you're inside the email body? 22
Well, if we look at the structure of this email, we know that there is a space between the header and 23
the body. 24
There is a new line right after the date information in the e-mail header. 25
So what we're gonna do is we're going to check for this blank right here. If we hit this blank line, 26
this new line, we're going to change our boolean variable to True. 27
So "elif line == '\n'" - "\n" is our 28
new line character. 29
So this checks for somebody having hit Enter on their keyboard essentially. 30
Then I'll add a semicolon at the end and then inside this else if statement I'm going to set is_body 31
equal to True. 32
And that's pretty much our loop. I can close my file after we've read all the individual lines in our 33
stream and then all I have to do is maybe print out my email body. So my "email_body" is 34
going to be equal to all of my list items maybe joined by a new line. 35
So I'll take "'\n'.join( 36
lines)". And now let's print out our email_body and hit Shift+Enter and what we see is that our 37
loop is working. 38
We've cut off our hair and we're left with just the email body. If you found this line of code confusing, 39
namely joining all the individual entries in the list with a new line character, 40
let's take it out and see how this formatting changes 41
if we don't have it. 42
So what I'll do is I'll put a hash tag there, comment this out and what I'll also do is comment out this 43
line right here 44
and instead I'm going to print out my list, which was lines. 45
Let's see what this looks like. Here's what we have. 46
We've got a new line character, 47
then we have the first line in our email - 48
"Dear Mr. Still", 49
then another new line character and everything is separated by a comma in this list as the loop 50
read it line by line and appended it to our list. 51
This is the formatting we would get straight out of our loop, and because this is not very readable, what 52
we can do instead is join all the individual entries in the list at the new Line character, and then 53
we get something much, much, much more readable. 54
Fantastic! 55
What we've just done is written the Python code to extract an email body from our raw data. With this 56
in hand, 57
we can apply this to all the messages in our dataset because they all follow the same pattern. 58
And I'm going to show you how to do just that in the next lesson. 59
Stay tuned.