所以我有這個類叫Person,基本上有構造函數的名稱,ID,年齡,位置,目的地,我想要做的是,當我想做一個新人,我希望它從一個txt文件打開。從txt調用定義的東西... Python
例如,這是我的Person類(在模塊中,人)
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()
所以基本上,而不是我不必手動鍵入初始化器「弗雷德,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]
確保只使用'file_handle.read()'和'str.split' –