Iterating over two text files in python?

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

Up vote 0 down vote favorite share g+ share fb share tw.

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 want use values from both files simultaneously: e.

G: c1 = x1*x2 + y1*y2 + z1*z2 #for first line c2 = x1*x2 + y1*y2 + z1*z2 #for second line How can one do that using Python? Python iteration textfiles link|improve this question edited Jul 19 '11 at 10:35Johnsyweb21.5k12348 asked Jul 19 '11 at 10:31Blessed111.

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.

1 In Python 3 the built-in function zip does the same. – Jacek Konieczny Jul 19 '11 at 10:58.

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 in zip(open('file1'), open('file2')): ... print zip(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.

Please note this returns a list, not iterator, in Python 2. That means, that the code will read whole files at once under Python 2.x. – Jacek Konieczny Jul 19 '11 at 10:57 Correct.

With such small files, it's unlikely to be a problem. But worth noting. – Johnsyweb Jul 19 '11 at 11:00.

This does the work for me: with open("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) be = 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, don't 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().

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions