for loop - Python: How to compare string from two text files and retrieve an additional line of one in case of match -


i have found information previous search on website seem stuck on following issue.

i have 2 text files looks

inter.txt ( n-lines showed 4 lines,you idea)

7275 30000 6693 855 .... 

rules.txt (2n-lines)

7275 8500 6693 7555 .... 3  1000 8 5 .... 

i want compare first line of inter.txt rules.txt , in case of match, jump n-lines in order score of line. (e.g. 7275, there match, jump n score 3)

i produced following code reasons, have ouput of first line when should have 1 each match first file. previous example, should have 8 output 6693.

import linecache  inter = open("inter.txt", "r") rules = open("rules.txt", "r")  iscore = 0 jump = 266 i=0 lineint in inter:     #i = i+1     #print(i)     linerul in rules:          = i+1          #print(i)          if lineint == linerul:              print("match")              inc = linecache.getline("rules.txt", + jump)              #print(inc)              iscore = iscore + int(inc)                       print(iscore)              #break          else:              continue 

all print(i) there because checked lines read. novice in python.

to sum up, don't understand why have 1 output. in advance !

ok, think main thing blocks getting forward loops on files gets pointer end of file, , doesn't resets when starts loops again.

so when open rules.txt once, , uses intance in inner loop goes through lines @ first iteration of outer loop, second time tries go on remains lines, non.

the solution close , open file outside inner loop. code worked me.

import linecache  inter = open("inter.txt", "r")  iscore = 0 jump = 4 lineint in inter:     i=0     #i = i+1     #print(i)     rules = open("rules.txt", "r")     linerul in rules:         = i+1         #print(i)         if lineint == linerul:             print("match")             inc = linecache.getline("rules.txt", + jump)             #print(inc)             iscore = iscore + int(inc)                      print(iscore)             #break         else:             continue      rules.close() 

i moved set 0 beginning of outer loop, guess you'd find yourself.

and changed jump 4 fit example files gave :p


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -