Total noob here.
Language: Python3
I am doing a beginners course on Coursera, Python for Everybody(specialization) And also following the free textbook provided by course instructor Dr. Charles Severance. Link to the textbook.
I am trying to use a for
loop in Python
to search through a text file. I want to search for a substring through the whole text file. I am trying to do in two different ways. Link to the text file I am using.
Sample one:
fhand = open('mbox-short.txt')
for lines in fhand :
if lines.startswith('From:') :
print(lines)
Sample two:
fhand = open('mbox-short.txt')
for lines in fhand :
if not lines.startswith('From:') :
continue
print(lines)
The output is the same for both pieces of code:
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
From: gsilver@umich.edu
From: gsilver@umich.edu
From: zqian@umich.edu
From: gsilver@umich.edu
From: wagnermr@iupui.edu
From: zqian@umich.edu
From: antranig@caret.cam.ac.uk
From: gopal.ramasammycook@gmail.com
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: david.horwitz@uct.ac.za
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: louis@media.berkeley.edu
From: ray@media.berkeley.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
From: cwen@iupui.edu
Now what I don't understand is what's the difference in using if
and if not
in the above-mentioned code.
What goes on under-the-hood when I use them.
How are they different?
When to use which?
PS: if I have made some mistakes in posting this question then please let me know, I would be happy to comply.
Thank you very much for taking the time to read all this. Any help would be very much appreciated.
Aucun commentaire:
Enregistrer un commentaire