I have over 10 text files, each file has exactly 2671 floats e.g.
1.232124234 #line 1 2.324234323 #line 2 . . 1.324234234 # line 2671
I would like to the add together the floats on each line with the float on the corresponding line for each of the 10 files, e.g. "file 1 line 1 float" + "file 2 line 1 float" +.....+ "file 10 line 1 float". I need to do this for each line in the text files and print the sum to a csv on 2671 lines.
I can achieve using the following very long code, but looking for a more efficient way of achieving the same outcome. As I will not always have the same number of text files for each time I do this. It could over 50 at times.
All of the files are labelled in number order as shown in the code below, which I think will help with this problem.
Here is my long version:
SumFile = open("SumFile.csv", "w") for s1, s2, s3, s4, s5, s6, s7, s8, s9, s10\ in zip(open('file1.txt', 'r'), open('file2.txt', 'r'), \ open('file3.txt', 'r'), open('file4.txt', 'r'), open('file5.txt', 'r'), open('file6.txt', 'r'), \ open('file7.txt', 'r'), open('file8.txt', 'r'), open('file9.txt', 'r'), open('file10.txt', 'r')): Sum = float(s1) + float(s2) + float(s3) + float(s4) + float(s5) + float(s6) +\ float(s7) + float(s8) + float(s9) + float(s10) SumFile.write(('%6.20f,\n') % (Sum))
I really would appreciate you help and suggestions.