python读取txt文件并将字符串分配给组合变量(串联)

时间:2016-10-06 23:10:32

标签: python file read-write

我有一个文本文件(成绩),我在这里给出了姓氏,名字和成绩。我需要读取它们,将它们分配给变量,并将它们写入不同的文本文件(Graded)。我是初学者,这是我的第一个新任务。我知道有错误,但我想知道的最重要的事情是如何读入然后写入文件,以及如何将3个变量合并为一个。

class Student:
    def __init__(self, str_fname, str_lname, str_grade):
        """Initialization method"""
        self.str_fname = str_fname
        self.str_lname = str_lname
       self.str_grade = str_grade

    @property
    def str_fname(self):
        return self.str_fname

    @str_fname.setter
    def str_fname(self, str_fname):
    """Setter for first name attribute"""
        if not isinstance(str_fname, str):
            #This is not a string, raise an error
            raise TypeError
        self.fname = str_fname

    @property
    def str_lname(self):
        """Return the name"""
        return self.str_lname

    @str_lname.setter
    def str_fname(self, str_lname):
        """Setter for last name attribute"""
        if not isinstance(str_lname, str):
            # This is not a string, raise an error
            raise TypeError
        self.str_lname = str_lname

    @property
    def str_grade(self):
        """Return the name"""
        return self.str_grade

    @str_grade.setter
    def str_grade(self, str_grade):
        """Setter for grade attribute"""
        if not isinstance(str_grade, str):
            # This is not a string, raise an error
            raise TypeError
        self.str_grade = str_grade


Student = str_lname+str_fname+str_fname
f = open('Grades.txt', 'r')
g = open('Graded.text', 'w')
f.read()
g.write()

1 个答案:

答案 0 :(得分:1)