我刚刚启动了python,我有大约6000个.txt文件,每个文件在列中包含少量数字,如:
file1.txt:
2
43
78
file2.txt:
98
12
and so on
我想读取它们并将它们存储在一个数组中并计算它的平均值。 平均值为(2,43,78,98,12 ..),即所有文件中的所有数字应给出1均值 当我阅读并存储它们时,它们看起来像:
['2,43,78','98,12',..]
...(我摆脱了'\ n')
但是当我使用ave = sum(a)\float(len(a))
时,我收到了错误消息。
我究竟做错了什么?
我有什么遗漏或其他方式吗?
代码:
import fnmatch
import os
rootPath = 'D:/Data'
pattern = '*.txt'
all_data = []
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
#print( filename )
name = os.path.join(root, filename)
str = open(name, 'r').read()
#print str
all_data.append(str)
a=[item.replace('\n', ' ') for item in all_data]
#print a
for val in a:
values = map(float, val.split(", "))
ave = sum(values)/len(values)
print ave
我收到错误:
float()
的文字无效
答案 0 :(得分:3)
sum("abc")
未定义。 sum("2, 43")
也不是。sum
。 float
仅适用于数字类型。
您需要首先拆分该行并将值转换为数值(我在此处使用sum
,因为float
将是len
,因此不需要要将float
转换为rows = ['2 43 78', '98 12']
total_sum = total_len = 0
for row in rows:
values = map(float, row.split())
total_sum += sum(values)
total_len += len(values)
print total_sum/total_len
):
print avg
对于Python 3.x,将print(avg)
替换为list()
,并在map
周围添加len
,否则public class All_Challenges {
public static void main(String[] args) {
System.out.println("Which class do you want to run?: ");
System.out.println("1. The first class");
Class[] theFiles = new Class[31];
theFiles[1] = Challenge_1_Whats_Your_Name.main(args);
theFiles[1].main(args);
}
}
未定义void
。
这与@VadimK中的his answer类似,但避免列表添加,而只是添加整数。
答案 1 :(得分:2)
使用列表理解的简单方法:
>>> my_list = ['2, 43, 78', '98, 12']
>>> my_nums = [float(j) for i in my_list for j in i.split(', ')]
>>> avg = sum(my_nums)/float(len(my_nums))
>>> avg
46.6
答案 2 :(得分:1)
我认为在阅读文件之后映射数字会更好:
total_list = []
for file in files:
str_list = file.read().splitlines() # ['1', '2', '3', '4', '5', '6']
int_list = map(int, str_list) # [1, 2, 3, 4, 5, 6]
total_list += int_list
ave = sum(total_list) / float(len(total_list))
答案 3 :(得分:0)
使用glob
for linux
import glob
tot_list=[]
for i in glob.glob('*.txt'): #Return a list of .txt files in current directory
# print('file:', i)
with open(i) as f: #Open file, read lines
lines = f.readlines()
for x in lines: # process each line
try:
x=int(x) #Test for integer value
tot_list.append(x) #Include in list
except:
pass
print('Total:',sum(tot_list),'No of Items:',len(tot_list))
print('Mean : %.2f' % (sum(tot_list)*1.0/len(tot_list))) #Print floating point result to 2 decimal places