Calculating The Quotient Between Two Files And Writing It Into Another File
Using Python, I have two large (equally long) files in which the numbers are divided by spaces: 0.11158E-13 0.11195E-13 0.11233E-13 ... # file1 0.11010E-13 0.11070E-13 0.11117E-13
Solution 1:
It looks like your files have one line each. So the easiest way to get the numbers in each file is to just read the entire content of the file, strip any unneeded characters and then split on the whitespace separating the floating point values. Splitting on the space will give you a list of strings
, which you can then coerce to floats
. At this point, you should have two lists of floating point values, and that is when you use the combination of the zip
and map
functions to carry out the division operation. The following is an illustration:
withopen('ex1.idl') as f1, open('ex2.idl') as f2:
withopen('ex3.txt', 'w') as f3:
f1 = map(float, f1.read().strip().split())
f2 = map(float, f2.read().strip().split())
for result inmap(lambda v: v[0]/v[1], zip(f1, f2)):
# This writes the results all in one line# If you wanted to write them in multiple lines,# then you would need replace the space character# with a newline character.
f3.write(str(result)+" ")
I hope this proves useful.
Post a Comment for "Calculating The Quotient Between Two Files And Writing It Into Another File"