0

I've written a program to parse info about an object from a file, but it consistently won't read the last item in the file list and I'm not sure why.

The object's compute method (the one that's having issues) depends on the following dictionary (abridged for minimality):

S_dict = {'Advanced Saucecrafting': 5, 'The Way of Sauce': 3}

The object itself is

class Character(object):

    def __init__(self, name, idnum):
        self.name = name
        self.idnum = idnum
        self.path = 'C:\\Users\\Bit\\Desktop\\Mafia\\data\\BtS_' + name + ' (#' + idnum + ')' + '.txt'
        self.SC = 0
        self.TT = 0
        self.P  = 0
        self.S  = 0
        self.DB = 0
        self.AT = 0
        self.skills = [self.SC, self.TT, self.P, self.S, self.DB, self.AT]
        self.SC_skills = []
        self.TT_skills = []
        self.P_skills  = []
        self.S_skills  = []
        self.P_skills  = []
        self.DB_skills = []
        self.AT_skills = []

    def compute(self):
        self.SC = 0
        self.TT = 0
        self.P  = 0
        self.S  = 0
        self.DB = 0
        self.AT = 0
        self.SC_skills = []
        self.TT_skills = []
        self.P_skills  = []
        self.S_skills  = []
        self.P_skills  = []
        self.DB_skills = []
        self.AT_skills = []

        with open(self.path, 'r') as f:
            text = f.read()
            perms = text.split(', ')
            for p in perms:
                try:
                    self.SC += SC_dict[p]
                    self.SC_skills.append(p)
                except KeyError:
                    pass
                try:
                    self.TT += TT_dict[p]
                    self.TT_skills.append(p)
                except KeyError:
                    pass
                try:
                    self.P += P_dict[p]
                    self.P_skills.append(p)
                except KeyError:
                    pass
                try:
                    self.S += S_dict[p]
                    self.S_skills.append(p)
                except KeyError:
                    pass
                try:
                    self.DB += DB_dict[p]
                    self.DB_skills.append(p)
                except KeyError:
                    pass
                try:
                    self.AT += AT_dict[p]
                    self.AT_skills.append(p)
                except KeyError:
                    pass
            self.skills = [self.SC, self.TT, self.P, self.S, self.DB, self.AT]

When I run compute it consistently, and robustly fails to read the last element in the .txt file I read from, a sample file for which it fails is below

Perfect Freeze, The Long View, Wisdom of the Elder Tortoises, Ambidextrous Funkslinging, Pulverize, Snokebomb, Subtle and Quick to Anger, The Way of Sauce, Torso Awaregness, Advanced Cocktailcrafting, Kung Fu Hustler, Cannelloni Cocoon, The Ode to Booze, Transcendental Noodlecraft, Master of the Surprising Fist, Spirit of Ravioli, Pastamastery, Advanced Saucecrafting

in this case the vector self.skills returns

[4, 8, 10, 3, 9, 6]

but self.skills[3] is a 3 when if you look in the S_dict you see that it possesses the keys 'Advanced Saucecrafting' and 'The Way of Sauce' which should give a combined total of 3 + 5 = 8. Additionally the attribute self.S_skills is simply ['Advanced Saucecrafting'] indicating that this part of the for loop never runs in the compute method which is more evidence to make me suspect the offending issue is hidden within.

4
  • Can you produce a minimal example? Commented May 27, 2016 at 20:01
  • Are you sure this is a minimal example, as defined in minimal reproducible example? Commented May 27, 2016 at 20:01
  • 1
    Try text = f.read() --> text = f.read().strip() Commented May 27, 2016 at 20:05
  • @Rogalski it should be as minimal as possible now without omitting things that might cause the problem. Commented May 27, 2016 at 20:05

1 Answer 1

2

The problem is that the last string (from the split) has a newline character, which causes the key lookup error. Try doing this instead:

perms = text.rstrip().split(', ')

Yielded this skills output for me:

[4, 8, 10, 8, 9, 6]
Sign up to request clarification or add additional context in comments.

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.