-2

How can I get the related lines from a file? This is my code:

read_file = "a.txt"  
read_batch = "b.txt"      

lines_batch = list()
with open(read_file) as r:
    bigstat = r.read()

with open(read_batch) as b:
    for batch in (line_batch.strip() for line_batch in b):
        if batch in bigstat:
            print(???)

Bigstat is a txt with 50 lines, but I only want the 2 of them, which contains the batch.

How should I do with this ? Thank you so much for the help!!!!!!

muc777
  • 37
  • 7
  • 1
    Forgot for a second that the data is coming from a file. If you just had a list of strings, would you know how to do this? – Carcigenicate Jan 26 '17 at 15:51
  • 1
    And specify what "related" means here. – Carcigenicate Jan 26 '17 at 15:51
  • use a for-loop? for line in ...? "related" means the lines which contains the words in batch – muc777 Jan 26 '17 at 15:58
  • So you need to find the lines in `a.txt` that are exactly the same as a line in `b.txt`? – Carcigenicate Jan 26 '17 at 16:01
  • I think your first problem is that you're making a lot of confusion with variable names. `lines_batch` is useless because you never put anything in it, and `bigstat` contains the entire file as a string, not as a list, so your condition is very inefficient. – Mikk Jan 26 '17 at 16:02
  • yes, bu not exactly the same, just the lines which "contains" the words in b.txt – muc777 Jan 26 '17 at 16:03
  • @Mikk yes....thank you for your suggestion – muc777 Jan 26 '17 at 16:07

1 Answers1

0

Here is some code that uses only a single for loop and a single if statement to check if a line in the read_file is present in the batch_file (I assume that is what you want to check!).

Just open the files and use readlines() to get all the lines individually. Then just iterate through all the lines in the read_file and check if they are in the list of lines in the batch_file (Note that readlines() generates a list whose individual entries are the contents of each line including the ending \n character).

read_file = "a.txt" 
batch_file = "b.txt" 

with open(read_file) as a: 
    a_lines = a.readlines() 

with open(batch_file) as b: 
    b_lines = b.readlines() 

for line in a_lines: 
    if line in b_lines: 
        print(line.strip())

Edit:

To get the number of the line in the read_file that contains the match to a line in the batch_file, you have to change the way you go through the read_file. In this case, use enumerate to get not only the content of each line but also the number of each line (in this case stored in the variable i).

I then just printed the number and content of the matched lines in the read_file and the batch_file.

i gets you the number of the line in the read_file.
a_lines[i] gets you the corresponding list item (= content of the line)
b_lines.index(line) gets you the number of the item line in the b_lines list (= number of the line in the batch_file)
line.strip() gets you the content of that line in the batch_file without the trailing \n character.

See the attached expanded code:

read_file = "a.txt" 
batch_file = "b.txt" 

with open(read_file) as a: 
    a_lines = a.readlines() 

with open(batch_file) as b: 
    b_lines = b.readlines() 

for i, line in enumerate(a_lines):
    if line in b_lines:
        print("Number of line in read_file is %i" % i)
        print("Content of line in read_file is %s" % a_lines[i].strip())

        print("Number of line in batch_file is %i" % b_lines.index(line))
        print("Content of line in batch_file is %s" % line.strip())
fumarat
  • 36
  • 5
  • Thank you so much! It really helps me a lot! May I ask you another question: How can I get the a_lines which contains the line. I tried with str.find and str.index and both of them just give me the position back... – muc777 Jan 27 '17 at 10:32
  • Hey, you are welcome! I have added some code in my original answer that should show you how to get not only the content of each line, but also the number of each line. And I would greatly appreciate if your problem is solved that you mark my answer as accepted! Just click on the green check mark on the left. – fumarat Jan 27 '17 at 11:29