0

I have 2 files: "a.txt" and "b.txt" where I want to match lines between them. The files contain the following:

1
2
3
4
5
6
7
8
9
10

To match the lines, I'm doing the following

    a = open("a.txt","r") 
    b = open("b.txt","r")
    for al in a:
        al = al.split()
        val_a = al[0]
        for bl in b:
            bl = bl.split()
            val_b = bl[0]
            print val_a, val_b

Surprisingly, the print statement ONLY prints the following:

1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10

Which appears to be that the loop on a is only accessed once. What I tried for debugging is the following:

for al in a:
    al = al.split()
    val_a = al[0]
    print val_a
    for bl in b:
        bl = bl.split()
        val_b = bl[0]

The print statement here prints all the values within a

Can someone help me with a possible explanation?

ifreak
  • 1,726
  • 4
  • 27
  • 45
  • 1
    A similar question is answered [over here](http://stackoverflow.com/questions/16095855/whats-the-most-pythonic-way-to-iterate-over-all-the-lines-of-multiple-files) – Muhammad Yaseen Khan Aug 02 '16 at 11:11
  • 1
    Also see http://stackoverflow.com/questions/13137969/python-reading-lines-in-nested-for-loops-from-two-files – PM 2Ring Aug 02 '16 at 11:17
  • I ended up using the "with" instead of the normal "open" statement which seems to work fine. – ifreak Aug 02 '16 at 13:31

4 Answers4

4

You need to reset the file pointer to the start of the file for b.txt each time you attempt to loop through it, otherwise you've reached the end.

The easiest way to do this is with file.seek(0) as shown below:

a = open("a.txt","r") 
b = open("b.txt","r")
for al in a:
    al = al.split()
    val_a = al[0]

    b.seek(0)

    for bl in b:
        bl = bl.split()
        val_b = bl[0]
        print val_a, val_b
moopet
  • 6,014
  • 1
  • 29
  • 36
3

You can fetch b to a list of lines with readlines(), and then you can iterate over it again and again:

a = open("a.txt","r") 
b = open("b.txt","r").readlines()
for al in a:
    al = al.split()
    val_a = al[0]
    for bl in b:
        bl = bl.split()
        val_b = bl[0]
        print val_a, val_b
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
1

try this :

a = open("a.txt","r")
b = open("b.txt","r")
for i,j in zip(a,b):
    print (i.split()[0])
    print (j.split()[0])

Explanation:

1)zip file will open both files simultanously
2)for loop will loop through line by line (i=one line in a-file, j=one line in b-file)
3)i.split()[0] will give first word/element of line
Vaibhav
  • 1,154
  • 10
  • 26
0

Convert b as a list else first iteration through b will consume the file.

 blist= list(b)

Then the inner loop

For bl in blist:
 ...
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219