Python列表,.txt文件

时间:2014-03-20 16:23:49

标签: python list text

我是初学者,我有几个问题。我有一个名称+成绩的.txt文件,例如:

Emily Burgess 5 4 3 4
James Cook 4 9 5 4
Blergh Blargh 10 7 2 4

我需要在新的.txt文件中写下他们的姓名,姓氏和成绩的平均值。然后我需要计算他们所有的平均成绩。我怎么做?我已经开始这样做,但我现在不知道该怎么做:

def stuff():
    things = []
    file = open(r'stuff2.txt').read()
    for line in file:
        things.append(line.split(' '))
    print(things)

    for grade in things:
        grades = int(grade[2], grade[3], grade[4], grade[5])
        average = grades/4
        print(average)

with open('newstuff.txt', 'w') as f:
    f.write(things)   

5 个答案:

答案 0 :(得分:1)

很难说,但看起来你的for循环中出现了一些问题。例如,您无法使用4个参数调用int构造函数:

TypeError: int() takes at most 2 arguments (4 given)

你的意思是:

grades = [int(g) for g in grades[1:]]
average = sum(grades) / len(grades[1:])

代替?

答案 1 :(得分:1)

编辑:既然你是一名初学的Python学生,我们暂时不会使用面向对象的编程,但我会保留下面的代码,以防你想要探索一下!

students = list() # initialize an accumulator list

with open("stuff2.txt") as infile:
    for line in infile:
        data = line.strip().split(" ")
        # strip removes ending and beginning whitespace e.g. the ending \n and etc
        datadict = {}
        datadict['first'] = data[0]
        datadict['last'] = data[1]
        datadict['grades'] = data[2:]
        students.append(datadict)
        # this can all be done in one line, but it's much clearer this way
# after this, all your students are in `students`, each entry in `students` is a
# dictionary with keys `first`, `last`, and `grades`.

# OUTPUT
with open("newstuff.txt","w") as outfile:
    for student in students:
        outputline = ""
        outputline += student['first']
        outputline += " "
        outputline += student['last']
        outputline += ": "
        outputline += ", ".join(student['grades'])
        # ", ".join(list) gives you a string with every element of list
        # separated by a comma and a space, e.g. ','.join(["1","2","3"]) == "1, 2, 3"
        outputline += "|| average: "
        average = str(sum(map(int,student['grades']))/len(student['grades']))
        # since student['grades'] is a list of strings, and we need to add them, you
        # have to use map(int, student['grades']) to get their int representations.
        # this is equivalent to [int(grade) for grade in student['grades']]
        outputline += average
        outputline += "\n"

        outfile.write(outputline)

        # again, this can be done in one line
        # outfile.write("{0['first']} {0['last']}: {1}||{2}\n".format(
        #              student, ', '.join(student['grades']), sum(map(int,student['grades']))/len(student['grades']))
        # but, again, this is long and unwieldy.

我总是支持为这些类型的应用程序使用类

class Student(object):
    def __init__(self,name=None,grades=None,initarray=None):
        """Can be initialized as Student(name="Name",grades=[1,2,3]) or
Student(["First","Last",1,2,3])"""
        if not (name and grades) or (initarray):
            raise ValueError("You must supply both name and grades, or initarray")
        if (name and grades):
            self.name = name
            self.grades = grades
        else:
            self.name = ' '.join(initarray[:2])
            self.grades = initarray[2:]

    @property
    def average(self):
        return sum(self.grades)/len(self.grades)

然后你可以做类似的事情:

students = list()

with open(r"stuff2.txt",'r') as f:
    for line in file:
        students.append(Student(line.strip().split(" ")))
# students is now a list of Student objects

您可以将它们全部写入文件:

with open("students_grades.txt","w") as out_:
    for student in students:
        out_.write(r"{student.name}: {45:grades}||{student.average}\n".format(
                  student=student, grades = ', '.join(student.grades)))

如果你想稍后再使用它们,你可能想要腌制你的物体。

import pickle

with open("testpickle.pkl","wb") as pkl:
    pickle.dump(students,pkl)

然后再次使用

import pickle # if you haven't already, obviously

with open('testpickle.pkl','rb') as pkl:
    students = pickle.load(pkl)

答案 2 :(得分:0)

您的代码可以像以下一样工作:

 with open('stuff2.txt') as f1, open('newstuff.txt', 'w') as f2:
    for line in f:
        raw_data = line.rstrip().split()
        average = sum(int(i) for i in raw_data[2:])
        new_data = ' '.join(raw_data[:2] + [str(average)])
        f2.write(new_data)

答案 3 :(得分:0)

假设原始的txt文件是stuff2.txt,并且你想要输出newstuff.txt:

def process_line(line):
    line = line.split()
    first = line[0]
    last = line[1]
    grades = [int(x) for x in line[2:]]
    average = sum(grades) / float(len(grades))
    return first, last, average

with open('stuff2.txt') as f:
    lines = f.readlines()
with open('newstuff.txt', 'w') as f:
    for line in lines:
        first, last, avg = process_line(line)
        f.write(first + " " + last + " " + str(avg) + "\n")

答案 4 :(得分:-1)

使用pandas:

import pandas
df = pandas.read_csv("stuff.txt", sep=" ", header=None, names=["first","last","grade1","grade2","grade3","grade4"])
df["average"] = (df["grade1"]+df["grade2"]+df["grade3"]+df["grade4"])/4.0
df.to_csv("newstuff.txt",sep=" ", index=False) #will print a header row, which you can disable with header=None