【发布时间】:2019-01-16 01:14:00
【问题描述】:
我有 10 个文本文件,每个文件都有多行和 3 列,由逗号 (',') 分隔。我的目标是计算每行 10 个文本文件之间的平均值,并且只使用第二列值。
例如:
-
1.txt:[1,2,3; 4,5,6; 7,8,9; ...] -
2.txt:[10,11,12; 13,14,15; 16,17,18; ...] -
3.txt:[19,20,21; 22,23,24; 25,26,27; ...]
我想要第二列值的平均值,比如说: A=(2+11+20)/3...然后 B=(5+14+23)/3...然后 C=(8+17+26)/3
因此我会得到[A;B;C] => 3x1 矩阵
目前我只能读取所有文件,但无法在我想要的数组中正确设置它们。
file_list = glob.glob(os.path.join(os.getcwd(), "Chl_96", "*.txt"))
corpus = []
for file_path in file_list:
with open(file_path) as f_input:
corpus.append(f_input.read())
print (corpus)
【问题讨论】:
-
对文件内容使用 split 命令。在分号上拆分以获取行。用逗号分割行以获得单个元素。
-
请edit您的问题并在文本文件中显示数据的实际格式——这很重要。
标签: python statistics