Skip to content Skip to sidebar Skip to footer

Iterating Over Two Text Files In Python

I have 2 text files and I want to iterate over both of them simultaneously. i.e: File1: x1 y1 z1 A,53,45,23 B,65,45,32 File2: x2 y2 z2 A,0.6,0.9,0.4 B,8.6,1.0,2.3 and I wa

Solution 1:

You need to treat both files as iterators and zip them. Izip will allow you to read the files in a lazy way:

from itertools import izip

fa=open('file1')
fb=open('file2')
for x,y in izip(fa, fb):
    print x,y

Now that you've got pairs of lines, you should be able to parse them as you need and print out the correct formula.

Solution 2:

Python's built-in zip() function is ideal for this:

>>>get_values = lambda line: map(float, line.strip().split(',')[1:])>>>for line_from_1,line_from_2 inzip(open('file1'), open('file2')):...printzip(get_values(line_from_1), get_values(line_from_2))...print'--'... 
[]
--
[(53.0, 0.6), (45.0, 0.9), (23.0, 0.4)]
--
[(65.0, 8.6), (45.0, 1.0), (32.0, 2.3)]
--
>>>

From that, you should be able to use the values as you wish. Something like this:

  print sum([x * y for x,y in zip(get_values(line_from_1), get_values(line_from_2))])

I get this result:

81.5

677.6

Solution 3:

This does the work for me:

withopen("file1.txt") as f1, open("file2.txt") as f2:
    # Ignore header line and last newline
    files = f1.read().split("\n")[1:-1]
    files += f2.read().split("\n")[1:-1]

# Split values and remove row name from lists# string -> float all values read
a1, a2, b1, b2 = (map(float, elem.split(",")[1:]) for elem in files)

# Group by row
a = zip(*[a1, b1])
b = zip(*[a2, b2])

c1 = sum(e1 * e2 for e1, e2 in a)
c2 = sum(e1 * e2 for e1, e2 in b)

Then results...

>>>print c1
81.5
>>>print c2
677.6

EDIT: If your Python version doesn't support with sorcery you can do:

# Open files, dont forget to close them!    
f1 = open("file1.txt")
f2 = open("file2.txt")

# Ignore header line and last newline
files = f1.read().split("\n")[1:-1]
files += f2.read().split("\n")[1:-1]

f1.close()
f2.close()

Solution 4:

All the examples given using (i)zip work fine if all your data is synchronised as the example. If they don't go in step - sometimes reading more lines from one than the other - the next() function is your friend. With it you can set up your iterators and then request a new line from either whenever you want in your program flow.

Post a Comment for "Iterating Over Two Text Files In Python"