在Python中从文本文件计算平均值
我有一个文本文件,里面有一串数字。现在我想计算每三组数字的平均值。有没有什么好主意可以做到这一点?谢谢!
文本文件中的数字示例:
5
7
3
10
12
6
我想要的输出结果:
5
9
4 个回答
1
data.txt
:
5
7
3
10
12
6
如何使用 numpy 来处理它:
In [4]: import numpy as np
In [5]: with open('data.txt') as f:
data = f.read().split()
...:
In [6]: data
Out[6]: ['5', '7', '3', '10', '12', '6']
In [7]: a = np.array(map(float, data))
In [8]: a
Out[8]: array([ 5., 7., 3., 10., 12., 6.])
In [9]: b = a.reshape([-1,3])
In [10]: b
Out[10]:
array([[ 5., 7., 3.],
[ 10., 12., 6.]])
In [11]: b.sum(1)/3
Out[11]: array([ 5. , 9.33333333])
1
你需要“解析”这个文本文件,为了做到这一点,你需要了解它是怎么组织的,以及它是用什么编码的。在此之前,我有几个问题想问你。
- 这些数字之间会一直有空格吗?
- 你希望输出结果在哪里?是打印到控制台上,还是放在一个新的文本文件里?
你可以这样做:
#read the file
my_file = open("C:/numbers.txt",'r')
my_text = my_file.read()
my_file.close()
#get a list of numbers from it (in string form)
my_text_numbers = my_text.split(' ')
#use list comprehension to get integer...also consider
#using map function
my_int_numbers = [int(n) for n in my_text_numbers]
#now the averaging part I will leave to you, I assume it's
#the file reading part you needed help with.
1
假设每一行都是单独的一行:
# text file is myData.txt
averages = {} # using a dictionary so you can keep track of the starting point
with open('myData.txt', 'r') as myFile:
data = myFile.read().split('\n') # this creates a list of the data
for index in range(len(data), 3): # step of 3
n = float(int(data[index]) + int(data[index+1]) + int(data[index+2])) / 3
averages[index] = n
如果列表的内容不是正好每三项一组,这段代码会出现一个叫做 IndexError
的错误。所以我加上了 try
和 except
这两个部分来处理这个问题:
# text file is myData.txt
averages = {}
with open('myData.txt', 'r') as myFile:
data = myFile.read().split('\n') # this creates a list of the data
for index in range(len(data), 3): # step of 3
try: a = int(data[index])
except (IndexError, TypeError): a = 0
try: b = int(data[index+1])
except (IndexError, TypeError): b = 0
try: c = int(data[index+2])
except (IndexError, TypeError): c = 0
# except (IndexError, TypeError): avoids an error if the end of the list
# has been reached or the line is not an integer
n = float(a + b + c) / 3
averages[index] = n