0

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.

  • I don't understand the question. Nowhere in your code do you have a `Person` object in a list. All you have is an empty list, with no `Person` inside, and for some unknown reason you think that this list should have a `name` and a `last_name` attribute. It's completely unclear to me what your `readFile` method is supposed to do. – Aran-Fey Sep 07 '18 at 06:18
  • Is there a specific problem you need help with? If your code works and you want to know how to improve your style, this question might be better suited to https://codereview.stackexchange.com/. There is one error i noticed at first glance, you open your file but you never close it. This will result in a memory leak. Have a look at the `with` statement https://stackoverflow.com/questions/1369526/what-is-the-python-keyword-with-used-for – sobek Sep 07 '18 at 06:18
  • Strings do not have method `append`, so `p.name.append(''.join(buffer))` should be `p.name += ''.join(buffer)`. Then again, you are doing it only once, so you can simply do `p.name = ''.join(buffer)`. Or, instead, you can replace `buffer.append(fileBuffer[i])` with `p.name += fileBuffer[i]`, although that would be theoretically slower. Also, you should return the result from `readFile`. Next problem is, you don't need `ReadFromFile` at all. It serves no purpose and in fact causes the problem that you never `close()` the file. – zvone Sep 07 '18 at 06:19
  • BTW, the whole file reading code could be: `with open("information.txt","r+") as f: p.name, p.last_name = f.read().split(' ', 1)` if there is always a space. – zvone Sep 07 '18 at 06:22
  • @Aran-Fey I tried to create a list of class objects but I couldn't so I wrote this code to give you guys an idea about what I'm trying to do. readFile method should parse the data and set name and last_name attributes of objects that supposed to be in list. Thanks for reply – Çağrı Kılıç Sep 07 '18 at 07:06
  • @sobek My code doesn't work, I wrote this code to give you guys a better view about what ı am trying to do and thanks for the close() issue I'll correct it immidiately – Çağrı Kılıç Sep 07 '18 at 07:09
  • You've successfully read the name and the last name from the file, so all you have to do is to create a `Person` instance with those attributes and then append it to your list. – Aran-Fey Sep 07 '18 at 07:11
  • @zvone p was supposed to be a list, not string but p.name+=''.join(buffer) gives me an idea about my problem. Thanks a lot – Çağrı Kılıç Sep 07 '18 at 07:14
  • @Aran-Fey I'll try, thanks a lot :) – Çağrı Kılıç Sep 07 '18 at 07:19

0 Answers0