3

I have a file with the attributes for an object on each line. I want to take the attributes, separated by spaces, and add them to my object. I want the objects in a list but I can't get it to work. I have put a comment next to the line that doesn't do what I think it should be able to do. Alternatively if I just pass the words list, it stores the entire list in the first attribute of the object.

class Brick(object):
    def __init__(self, xopos=None, yopos=None, xcpos=None, ycpos=None, numb=None, prop=None):
        self.xopos = xopos
        self.yopos = yopos
        self.xcpos = xcpos
        self.ycpos = ycpos
        self.numb = numb
        self.prop = prop

bricklist = []

with open('data.txt', 'r') as f:
    data = f.readlines()

for line in data:
    words = line.split()
    bricklist.append(Brick.xopos(words[0])) #line that doesnt work

for i in range(len(bricklist)):
    print (bricklist[i].xopos)

the data is simply

1 13 14 15 16 17
2 21 22 23 24 14
3 3 4 5 6 7
4 1 1 1 1 1
5 5 6 4 1 1 
6 5 6 8 4 2
7 4 9 7 5 6 

I am very new to python, and I am finding alot of my ideas for implementing things just don't work so any help would be much appreciated.

7
  • fix your indentation please Commented Jul 15, 2015 at 15:36
  • 2
    What are you actually trying to achieve? Does bricklist.append(Brick(*words)) do the trick? Commented Jul 15, 2015 at 15:36
  • print out word[0] to see if that is the data you need? Commented Jul 15, 2015 at 15:42
  • That does work! Thanks a bunch, I'm trying to create a list of objects whose attributes are populated by the input file, I will go and research why that works. Commented Jul 15, 2015 at 15:43
  • 2
    Brick.xopos(words[0]) is nonsense given the code that we can see at the moment. First, the class object Brick does not have an attribute named xopos - your __init__ function creates instance attributes, not class attributes (i.e self.xopos, not xopos). Second, chances are that, even if the attribute did exist, it's not a callable (the default None valuable certainly isn't), so Brick.xopos(...) makes no sense. Commented Jul 15, 2015 at 15:54

2 Answers 2

1

Try this:

class Brick(object):
    def __init__(self, values):
        self.xopos = values[0]
        self.yopos = values[1]
        self.xcpos = values[2]
        self.ycpos = values[3]
        self.numb = values[4]
        self.prop = values[5]

bricklist = []

with open('data.txt', 'r') as f:
    for line in f.readlines()
        bricklist.append(Brick(line.split())

for brick in bricklist:
    print (brick.xopos)

Instead of passing each attribute individually, read each line from the file, split it into a list and pass that to the constructor of your Brick object.

You can improve the __init__ method by verifying the content of values before using it.

Sign up to request clarification or add additional context in comments.

Comments

0

I recommend introducing a function which takes in a string (a line of text in this case) and creates a Brick object from it:

class Brick(object):
    def __init__(self, xopos=None, yopos=None, xcpos=None, ycpos=None, numb=None, prop=None):
        self.xopos = xopos
        self.yopos = yopos
        self.xcpos = xcpos
        self.ycpos = ycpos
        self.numb = numb
        self.prop = prop

    @classmethod
    def from_string(cls, s):
        values = [int(v) for v in s.split()]
        return cls(*values)


with open('data.txt', 'r') as f:
    bricklist = [Brick.from_string(line) for line in f]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.