text - In PYTHON would it be possible to write a program which reads the data from a .txt file? -


in python possible write program reads data .txt file , turn data lists, make calculations, etc...

for example if had .txt file read:

 line1| 2011     18.5  11.8  19.7  31.6  26.6  37.3  37.4  24.6  34.0  71.3  46.3  28.4  line2| 2012     55.2  92.0  87.5  81.0  83.5  79.6 115.1 112.7 115.7 112.7 136.2 127.4 

could compute average numbers assigned each year? (note: i'm using version 3.4)

you can open file , read lines , split them , convert them list. can use numpy calculate mean. hope following code helps

import numpy np text_f = open('file.txt').readlines() text_f.remove('\n') arrays= [] in text_f:     new = i[6:]     arrays.append(list(map(float, new.split()))) avrgs = {} in arrays:     avrgs[i[0]] = np.mean(i[1:]) avrgs 

output:

{2011.0: 32.291666666666664, 2012.0: 99.88333333333334} 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -