so i am very very new to python. need some basic help.
my logic is to find words in text file.
party A %aapple 1
Party B %bat 2
Party C c 3
i need to find all the words starts from %.
my code is
searchfile = open("text.txt", "r")
for line in searchfile:
for char in line:
if "%" in char:
print char
searchfile.close()
but the output is only the % character. I need the putput to be %apple and %bat
any help?
You are not reading the file properly.
searchfile = open("text.txt", "r")
lines = [line.strip() for line in searchfile.readlines()]
for line in lines:
for word in line.split(" "):
if word.startswith("%"):
print word
searchfile.close()
You should also explore regex to solve this as well.