我有.csv
个文件,试图依次获取每3行的平均值。表示第1至3行,然后是第4至6行,等等。这是一个小例子:
2
5
8
11
14
17
20
23
26
我期望得到的输出是:
5
14
23
我试图在python中做到这一点,并编写了以下代码。它不会返回我的期望:
infile = open("finename.csv", "r")
sum = 0
for i in range(len(infile)):
if i+(i+1)+(i+2) %3 = 0,
sum += infile[i]
average = sum/3
您知道如何解决吗?
答案 0 :(得分:1)
输入文件:
$ cat a
2
5
8
11
14
17
20
23
26
30
代码:
counter = 0
sum_value = 0
with open('a', 'r') as fh:
for line in fh:
line = line.rstrip()
sum_value += int(line)
counter += 1
if counter == 3:
print ('%d' % (sum_value / counter))
sum_value = 0
counter= 0
输出:
5
14
23
答案 1 :(得分:1)
您想要这样的东西吗?
import csv
import io
with open("file.csv") as csvFile:
nbline = 0
moy = 0
listMoy = []
reader = csv.reader(csvFile)
for line in reader:
moy = moy + int(line[0])
nbline = nbline + 1
if nbline == 3:
moy = moy/3
print moy
listMoy.append(moy)
moy = 0
nbline = 0
with io.open("fileOut.csv", "w", encoding='UTF-8') as fichier:
for moy in listMoy:
fichier.write(u"{0}\n".format(moy))
答案 2 :(得分:0)
您的if语句很奇怪,您正在检查3 * i + 3是否为3的倍数,这种情况总是如此。 我会给您的步长arg以3乘3的方式。
infile = open('file.csv', 'r').readlines()
for i in range(2, len(infile), 3):
# starting at two to be sure to have always 3 values to check
sum = int(infile[i-2]) + int(infile[i-1]) + int(infile[i])
average = sum/3
print(average)
答案 3 :(得分:0)
with open(filename) as infile:
lines = infile.readlines()
for i in range(0, len(lines), 3): #Split readline into subset of 3 elements
val = map(int, lines[i:i+3]) #Convert elements in list to int
print(sum(val)/3) #Get Avg
输出:
5
14
23
无需读取内存中的完整文件。谢谢@brunodesthuilliers。
res = []
temp = []
c = 0
with open(filename) as infile:
for line in infile:
if c == 3:
res.append(sum(temp)/3)
temp = []
c = 0
temp.append(int(line))
c += 1
if temp:
res.append(sum(temp)/3)
print(res)