Reputation: 1
Like i have a text file abc.txt and it is like this
we 2 rt 3 re 3 tr vh kn mo
we 3 rt 5 re 5 tr yh kn me
we 4 rt 6 re 33 tr ph kn m3
we 5 rt 9 re 34 tr oh kn me
we 6 rt 8 re 32 tr kh kn md
now i want the values against the tr and after filtering it should get this result
[vh,yh,ph,oh,kh]
can anyone tell how to do it.what code should be write for it
Upvotes: 0
Views: 240
Reputation: 1522
One may try the following:
def filter_words(filename, magic_word):
with open(filename) as f:
all_words = f.read().strip().split()
filtered_words = []
i = 0
while True:
try:
i = all_words.index(magic_word, i) + 1
filtered_words.append(all_words[i])
except IndexError, ValueError:
break
return filtered_words
This algorithm does not fail in case 'tr' happens to be the last word in the provided text file.
Example:
>>> filter_words('abc.txt', 'tr')
['vh', 'yh', 'ph', 'oh', 'kh']
Upvotes: 0
Reputation: 27575
Wouldn't be simpler to use a regex ?
If 'we' , 'rt' , 're' , 'tr' are really constant at their places :
import re
ch = '''
we 2 rt 3 re 3 tr vh kn mo
we 3 rt 5 re 5 tr yh kn me
we 4 rt 6 re 33 tr ph kn m3
we 5 rt 9 re 34 tr oh kn me
we 6 rt 8 re 32 tr kh kn md'''
print re.findall('(?<= tr )([^ ]+)',ch)
If not, and then the position being the criterium to determine what to catch:
import re
ch = '''
we 2 rt 3 re 3 tr vh kn mo
we 3 rt 5 re 5 tr yh kn me
we 4 rt 6 re 33 tr ph kn m3
we 5 rt 9 re 34 tr oh kn me
we 6 rt 8 re 32 tr kh kn md'''
print [ mat.group(1)
for mat in re.finditer('^(?:\w+ \d+ ){3}\w+ ([^ ]+) .+',ch,re.M)]
Upvotes: 0
Reputation: 9687
You can split the lines as before tr
and after tr
and obtain the first word in the second part.
[ line.split(' tr ')[1].split()[0] for line in file ]
If there is more than one tr
, the expression collects the word after the first one. Alternatively, this one collects the words after the last tr
in a line:
[ line.split(' tr ')[-1].split()[0] for line in file ]
Upvotes: 2
Reputation: 336128
mylist = [line.split()[7] for line in myfile]
should work if it's always the 8th column.
If the position of tr
is variable, you could do
mylist = []
for line in myfile:
items = line.split()
mylist.append(items[items.index("tr")+1])
Upvotes: 2
Reputation: 5343
from operator import itemgetter
# tr value is in the 8th column
tr = itemgetter(7)
print map(tr, (line.split() for line in myfile.readlines()))
Upvotes: 0
Reputation: 8068
If I understand correctly, something like this should do the job (not tested):
resultArray = []
for aString in yourFile:
anArray = aString.split()
for i in range(0, len(anArray) - 1): //-1 in case tr is at the end of array
if anArray[i] == 'tr':
resultArray.append(anArray[i + 1])
Upvotes: 0
Reputation: 601529
Your question is not quite clear. Does this what you are after?
[line.split()[7] for line in open("abc.txt")]
It returns the eighth "word" from every line.
Upvotes: 1