0
Printing the lines that contain the substring 'snake'
#opening the text file f= open("Acrochordidae.txt", "r") for data in f: lines= f.readlines() if lines == "snake": newline= f.readline() else: break print(newline) I tried to format it in different ways but this is how far I got.
8 Réponses
0
lines = open('yourfile.txt', 'r').readlines()
matches = [line for line in lines if line.lower().find('snake') != -1]
print(matches)
+ 10
⇨ Follow community RULES: https://www.sololearn.com/Content-Creation-Guidelines/
// --> 2nd Post!!
https://www.sololearn.com/Discuss/1790165/?ref=app
+ 2
for data in f will give you the lines in f, no need to use lines= f.readlines(), especially not in the for loop
if lines == "snake" will check if the whole line equals "snake", not if "snake" is somewhere in the line (use if substring in string)
newline= f.readline() doesn't make sense
else: break will break the whole for loop as soon as "snake" is not in a line
+ 1
So you want to
1.Open a file
2.Print out lines that contain "snake" right
+ 1
And I'm sorry if my code didn't make sense. I'm still trying to understand file handling. I will try to get better.
0
yes
0
I just need understanding how to format it right. Like finding the substring.
0
Thank you. I will try if it works.