问题
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?
回答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.
回答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 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
回答3:
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])
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()
回答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.
来源:https://stackoverflow.com/questions/6745746/iterating-over-two-text-files-in-python