Read a file line-by-line instead of read file into memory

后端 未结 4 1289
盖世英雄少女心
盖世英雄少女心 2021-01-29 00:28

I´m having some trouble with reading a file line-by-line, instead of reading the the file into memory. As of right now, I\'m reading the file into memory, and it works perfect.

4条回答
  •  一生所求
    2021-01-29 01:25

    Is this right for you?

    with open(filename, 'r') as f:
        data = f.readlines()
    
    A = sum(float(line) for line in data)
    B = sum(float(line)**2 for line in data)
    

提交回复
热议问题