从.txt文件中读取数据并计算平均值

时间:2019-04-09 16:55:32

标签: python-3.x

如何计算具有多列的.txt文件的平均值

with open(ns) as np:

       next(np)
       values = np.read().splitlines()
       for line in values:
       splitline = line.split(" ")
       average = float(splitline[-4] * int(splitline[-3]) / 4

1 个答案:

答案 0 :(得分:0)

您可以使用str.split()在空间上分割每一行,然后选择所需的值。

我没有测试这段代码,但是类似的东西应该可以工作

with open(name_of_share) as fp:  
   for line in fp:
     parts = line.split()
     price = float(parts[3])
     purchase = int(parts[4])
     average = float(price * purchase / 4)