所以我有一个名为 Person 的类,它基本上具有构造函数名称、id、年龄、位置、目的地,我想做的是当我想创建一个新人时,我希望它从一个 txt 文件中打开。
例如,这是我的 Person 类(在 Module,People 中)
class Person :
def __init__(self, name, ID, age, location, destination):
self.name = name
self.ID = ID
self.age = age
self.location = location
self.destination = destination
def introduce_myself(self):
print("Hi, my name is " + self.name + " , my ID number is " + str(self.ID) + " I am " + str(self.age) + " years old")
import People
Fred = People.Person("Fred", 12323, 13, "New York", "Ithaca")
Fred.introduce_myself()
所以基本上,我不必手动输入初始化程序“fred,12232”等。我希望它从一个已经写入所有内容的 txt 文件中读取。
这是txt文件中的内容
[Name, ID, Age, Location, Destination]
[Rohan, 111111, 28, Ithaca, New Caanan]
[Oat, 111112, 20, Ithaca, New York City]
[Darius, 111113, 12, Los Angeles, Ithaca]
[Nick, 111114, 26, New Caanan, Ithaca]
[Andrew, 111115, 46, Los Angeles, Ithaca]
[James, 111116, 34, New Caanan, Ithaca]
[Jennifer, 111117, 56, Los Angeles, New Caanan]
[Angela, 111118, 22, New York City, Los Angeles]
[Arista, 111119, 66, New Caanan, Los Angeles]