简体   繁体   English

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

[英]python read in a txt file and assign strings to combined variable (concatenation)

I have a text file (Grades) where I'm given last name, first name, and grades.我有一个文本文件 (Grades),其中包含姓氏、名字和成绩。 I need to read them in, assign them to variables, and write them to a different text file (Graded).我需要读入它们,将它们分配给变量,然后将它们写入不同的文本文件(已分级)。 I'm a beginner and this is my first hw assignment.我是初学者,这是我的第一个硬件作业。 There are errors I know, but the biggest thing I want to know is how to read in and then write to the file, as well as how to combine the 3 variables into one.我知道有错误,但我最想知道的是如何读入然后写入文件,以及如何将 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM