I'm newbie in Python and I'm stuck in list of an class objects thing. I'm familiar with oop concept. I'm trying to get some data from a file and I want to change name, last_name and age attributes of an object individually. I know I probably can do that by creating a Person object and setting that object's attributes than append that object to the list but I was wondering is there a way to do that something like this:
class Person():
def __init__(object):
name=""
last_name=""
class ReadFromFile():
def readFile(self):
self.file=open("information.txt","r+")
fileBuffer=self.file.read()
p =[ ]
buffer = list()
for i in range(len(fileBuffer)):
while fileBuffer[i] != ' ':
buffer.append(fileBuffer[i])
i+=1
i+=1 #Space character will be passed
p.name.append(''.join(buffer))
buffer.clear()
while fileBuffer[i] != ' ':
buffer.append(fileBuffer[i])
i+=1
i+=1 #Space character will be passed
p.last_name.append(''.join(buffer))
buffer.clear()
def main():
reader = ReadFromFile()
reader.readFile()
if __name__ =="__main__":
main()
In this code, I'm trying to get the data character by character, all characters before the first white space char, will be name and everything after white space will be last_name. English is not my native language so I might done some mistakes. Please let me know if you don't get something and you wanna help.