Reading from input file.
David Murmann
david.murmann at rwth-aachen.de
Wed Jan 11 15:44:35 EST 2006
hi!
i find it rather hard to understand your problem, but i'll try anyway:
paczkow at gmail.com schrieb:
> So.. for my future Python script, the input data are in form of:
> 1
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> 2
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> 1233.2E-3 2123.2323 2E+2 3453.3E+1
> and so on...
are these 1's and 2's there to indicate different files, or do
they really appear in the input files?
> The number 1 and 2 and so on, are the numbers of measurments, which are
> different in number depending on analyzed case. So sometimes it could
> be 3 and sometimes 1000 or more.
>
> What I want to achieve, I want to compare or rather get difference
> between two similar input files. I tried to work with "readline"
> command, however it is hard to use it (for me) since input files are
> different in its size (3,1000 or more input points). Therefore, I
> decided to ask you for help how to do it. Esspecially how to make that
> python script which will be reading data, each line, and each column
> of input file and therefore will compare it with different input file
> giving finally difference size between each measures for example:
> First data at point 1 minus first data from point 1 from different
> input file:
> (1233.2E-3) - (1233.3E-3) = -0.1E-3
well, from what i understood you might try something like this:
-------------------------------
from itertools import izip
# iterate over two files at once (stopping at the shorter ones end)
for lines in izip(file('inputfile1'), file('inputfile2')):
# split the lines into columns
columns = [line.split() for line in lines]
# calculate and print the difference of the individual entries
for x, y in izip(*columns):
print float(x)-float(y),
print
-------------------------------
if this (or something similar) does not work for you, you could take
a look at the difflib module:
http://docs.python.org/lib/module-difflib.html
hope that helps, David.
More information about the Python-list
mailing list