i made a file reader but it always returns corrupted:

def load(charactor):
    fout = open(charactor + '.actor', 'r')
    fout.seek(0)
    actor_type = None
    attrs = []
    for index, line in enumerate(fout.readlines()):
        if index == 0:
            actor_type = line.replace('{', "").strip()
        if index == 1:
            if line == 'attributes':
                if not line == '}':
                    val = line.replace('\"', '').split('=').strip()
                    attrs.append(val)
            elif line == 'extras':
                if not line == '}':
                    val = line.replace('\"', '').split('=').strip()
                    attrs.append(val)
            else:
                print 'Corrupt file: ' + charactor + '.actor'
                break
    return str(actor_type), attrs
    fout.close()


var = load('person')

any idea? i want it to succede not be corrupted

Recommended Answers

All 4 Replies

Well give us a look at an example file that you are using. That will help greatly.

actor.py(the class where the function came from:

class Actor(object):
    def __init__(self, name, age, race, profession):
        self.attributes = {
            "name":name,
            "age":age,
            "race":race,
            "profession":profession,
        }
        self.attrs = ["name", "age", "race", "profession"]
		
	def loadAttributes(self, charactor):
		fout = open('charactors/' + filename + '.actor', 'r')
		fout.seek(0)
		actor_type = None
		attrs = []
		for index, line in enumerate(fout.readlines()):
			if index == 0:
				actor_type = line.replace('{', "").strip()
			if index == 1:
				if line == 'attributes':
					for index, line in enumerate(fout.readlines(3, 6)):
						if not line == '}':
							val = line.split('=').strip()
							attrs.append(val)
				elif line == 'extras':
					for index, line in enumerate(fout.readlines(9, 14)):
						if not line == '}':
							val = line.split('=').strip()
							attrs.append(val)
				else:
					print 'Corrupt file at line: ' + line + 'in' + charactor + '.actor'
					break;
		fout.close()
		
    def printAttrs(self):
        for item in self.attrs:
            print repr(self.attrs.index(item)) + ') ' + self.attributes[item]

    def changeAttr(self, attr, val):
        self.attributes[attr] = val

    def addAttr(self, attr, val):
        self.attributes[attr] = val

person.actor(the file to be read):

Citizen {
	attributes {
		name = "tom"
		age = "15"
		race = "human"
		profession = "assassin"
	}
	extras {
		awesomness = "2000"
	}
}

Hmm i think i see what you are doing, personally what i would do is make a python program that made an actor and then pickled it rather then using what you are doing, that way you could unpickle it in the actor class and everything would be fine and dandy.

But its all up to you, i think its going to be quite hard that way.... unless you want to use something like eval() to make a dictionary.. that might just work..

ok ill do taht

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.