I have identified a line in a text file that looks like this:
FLAGS = WORD1 WORD2 WORD3
I am reading several files in which the number of words can vary from 0 to a maximum of 3.
I'm using this code:
flag_FLAG = 0
for i in range(len(materialfile)):
if "FLAG" in materialfile[i] and "=" in materialfile[i]:
line_FLAG = i
flag_FLAG = 1
if flag_FLAG == 1:
temp = materialfile[line_FLAG].split(" ")
for elem in temp:
if is_word(elem):
flags = str(elem)
unfortunately this way I only get one word (the last one). "is_word" is a function that i creat:
def is_word(s):
try:
str(s)
return True
except ValueError:
return False
I would like to get all the words as targets. I hope I have been clear.
You want a nested loop, e.g.:
Hard to say whether this exact code will work with your actual file, since you didn't provide a sample file, but hopefully this gets you pointed in the right direction.
Note that your
is_wordfunction does nothing since these are already strings and will hence always convert tostr()as a no-op without raising an exception. Theif flagin the above comprehension will filter out values offlagthat are empty (e.g. if you had a line likeFLAGS =).