取一个文件的列平均值,对所有.txt文件重复,将所有平均值写入python中的一个文件

取一个文件的列平均值,对所有.txt文件重复,将所有平均值写入python中的一个文件,python,string,average,multiple-columns,glob,Python,String,Average,Multiple Columns,Glob,我有大约40个*.txt文件,它们都包含两列数据——它们已从以前的脚本转换为str格式,并以空格分隔。我想为每个.txt文件取第二列的平均值,然后将所有平均值放入一个输出文件中。它们还需要以数字顺序读取,例如file1.txt、file2.txt 我现在有以下脚本a)读取所有.txt文件的最后一行,b)取一个文件的平均值,但是当我尝试组合它们时,我要么得到一个错误,说它无法将字符串转换为浮点值,要么列表索引超出范围。我还尝试使用line.strip()方法来确认.txt文件中没有空行来解决后一个

我有大约40个*.txt文件,它们都包含两列数据——它们已从以前的脚本转换为str格式,并以空格分隔。我想为每个.txt文件取第二列的平均值,然后将所有平均值放入一个输出文件中。它们还需要以数字顺序读取,例如file1.txt、file2.txt

我现在有以下脚本a)读取所有.txt文件的最后一行,b)取一个文件的平均值,但是当我尝试组合它们时,我要么得到一个错误,说它无法将字符串转换为浮点值,要么列表索引超出范围。我还尝试使用line.strip()方法来确认.txt文件中没有空行来解决后一个问题,但没有效果

a) 读取所有.txt文件最后一行的代码:

import sys
import os
import re
import glob

numbers = re.compile(r'(\d+)')
def numericalSort(value):
    parts = numbers.split(value)
    parts[1::2] = map(int, parts[1::2])
    return parts

list_of_files = glob.glob("./*.txt")

for file in sorted(list_of_files, key=numericalSort):
    infiles =  open(file, "r")

    outfile = open("t-range","a")

    contents = [""]
    columns = []

   counter = 0

   for line in infiles:
        counter += 1
        contents.append(line)

    for line in contents:
        if line.startswith("20000"):
            columns.append(float(line.split()[1]))
            print columns

    counter1 = 0
    for line in columns:
        counter1 += 1
      outfile.write(','.join(map(str,columns))+"\n")

infiles.close()
outfile.close()
b) 获取一个文件的平均值的脚本:

data = open("file.txt","r").read().split()
s = sum ([float(i) for i in data])
average = s / len(data)
c) 组合脚本

import sys
import os
import re
import glob

numbers = re.compile(r'(\d+)')
def numericalSort(value):
    parts = numbers.split(value)
    parts[1::2] = map(int, parts[1::2])
    return parts

list_of_files = glob.glob("./totalenergies*")

for file in sorted(list_of_files, key=numericalSort):
    infiles =  open(file, "r")
    outfile = open("t-range","a")

    contents = [""]
    columns = []

    counter = 0

    for line in infiles:
        counter += 1
        contents.append(float(line.split()[1]))

        contents = ([float(i) for i  in contents])
        s = sum(contents)
        average = s / len(contents)
        columns.append(average)

        counter1 = 0
        for line in columns:
            counter1 += 1
            outfile.write("\n".join(map(str,columns)) + "\n")


infiles.close()
outfile.close()

最后一部分给出的错误为无法将字符串转换为浮点-回溯显示contents=([float(i)for i in contents])存在问题。

这是给出错误的行:

outfile.write("".join(map(str,columns)+"\n"))
当您阅读回溯错误时,最后一部分通常会显示脚本中生成问题的行号,因此您应该首先检查该行号

行当前读取的方式,
+“\n”
连接
函数的一部分,而它应该是
写入
方法的一部分:

outfile.write("".join(map(str,columns)) + "\n")
如果您打算将
中的每个平均值写入新行,并在列表末尾插入新行,则需要:

outfile.write("\n".join(map(str,columns)) + "\n")

更多信息:

谢谢!尽管回溯错误是因为我需要将contents=([float(str(I))用于contents中的I])