Ble*_*sed 0 python iteration text-files
我有2个文本文件,我想同时迭代它们.
即:
文件1:
x1 y1 z1
A,53,45,23
B,65,45,32
Run Code Online (Sandbox Code Playgroud)
文件2:
x2 y2 z2
A,0.6,0.9,0.4
B,8.6,1.0,2.3
Run Code Online (Sandbox Code Playgroud)
我想同时使用两个文件中的值:
例如:
c1 = x1*x2 + y1*y2 + z1*z2 #for first line
c2 = x1*x2 + y1*y2 + z1*z2 #for second line
Run Code Online (Sandbox Code Playgroud)
如何使用Python做到这一点?
您需要将这两个文件视为迭代器并压缩它们.Izip将允许您以懒惰的方式阅读文件:
from itertools import izip
fa=open('file1')
fb=open('file2')
for x,y in izip(fa, fb):
print x,y
Run Code Online (Sandbox Code Playgroud)
既然你已经有了成对的行,你应该能够根据需要解析它们并打印出正确的公式.