-1
我有一個文本文件(成績),我給了姓,名和成績。我需要閱讀它們,將它們分配給變量,並將它們寫入不同的文本文件(Graded)。我是初學者,這是我的第一個任務。我知道有些錯誤,但我想知道的最重要的事情是如何讀入然後寫入文件,以及如何將3個變量合併爲一個。在一個txt文件中讀取python並將字符串分配給組合變量(連接)
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()
是的,這是非常簡短的,但我想同時閱讀,然後分配給變量和寫。我在任何地方都找不到像那樣的例子。 –