0

I'm new to working with JSON in Python. I have a program where I generate 10 characters (objects) that have several properties. I'm write these to a file using the Python JSON library:

def saveCharsToFile(gameChars):
# Using a WITH operator for easy readability. creating and opening a file
# in write mode
with open("characterFile.json", "w") as write_file:
    # using the json.dump function to take the objects in the gameChars
    # list and serialise it into JSON
    json.dump([x.__dict__ for x in gameChars], write_file)
    # Tells the User that the file has been written

print("File 'characterFile.json' has been written to...")

Here is an example of one of the types of classes that generate a character:

# A subclass for creating a Wizard character, inherits the Character class
# and adds the power, sAttackPwr, and speed properties
class wizard(character):
    def __init__(self, CharPower, CharSAttackPwr, CharSpeed):
        # Getting the properties from the inheritted character Base Class
        character.__init__(self, "W", 100)
        self.power = CharPower
        self.sAttackPwr = CharSAttackPwr
        self.speed = CharSpeed


# Base Class for creating an RPG character
class character:
    # __init__ method, creates the name, type, health properties
    def __init__(self, charType, charHealth):
        self.name = rndName()
        self.type = charType
        self.health = charHealth

Here is the contents of a file saved using the saveCharsToFile:

[{"name": "ing low fu ", "type": "B", "health": 100, "power": 60, "sAttackPwr": 10, "speed": 60}, {"name": "en ar da ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "ar fu fu ", "type": "B", "health": 100, "power": 70, "sAttackPwr": 20, "speed": 50}, {"name": "da low en ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "da fu cha ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "da ing el ", "type": "W", "health": 100, "power": 50, "sAttackPwr": 70, "speed": 30}, {"name": "cha kar low ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "ar da el ", "type": "D", "health": 100, "power": 90, "sAttackPwr": 40, "speed": 50}, {"name": "da da ant ", "type": "B", "health": 100, "power": 60, "sAttackPwr": 10, "speed": 60}, {"name": "el ing kar ", "type": "B", "health": 100, "power": 70, "sAttackPwr": 20, "speed": 50}]

I'd like to be able to read this back into the program, as a list, preferably instantiating them as objects with the values for the character properties in that list above.

This is the current way I'm reading the file back into the program:

def openCharsToFile(gameChars):
    with open("characterFile.json") as read_file:
        y = json.load(read_file)
        for x in y:
            gameChars.insert[x, y]

        for z in gameChars:
            print(z.getStats())

But when I run this program, I get the following error:

Traceback (most recent call last):
    File "rpgProblemSolving.py", line 342, in <module>
        main()
    File "rpgProblemSolving.py",  line 338, in main
        openCharsToFile(gameChars)
    File "rpgProblemSolving.py", line 305, in openCharsToFile
        gameChars.insert[x, y]
TypeError: 'builtin_function_or_method' object is not subscriptable

If anyone has some suggestions, It would be most appreciated thanks

0

1 Answer 1

1

I believe you wanted to just add your character to existing characters, so use append instead. Your error was that you had [x, y] instead of (x, y), but even if we fix that that makes no sense since first argument (x) of insert is index of list (gameChars) where your y will be inserted, which makes no sense since x is your character and y is your dict.

Also part where you print your characters print(z.getStats()) won't work since gameChars contains only characters in form of dictionaries that of course doesn't have such method as getStats().

def openCharsToFile(gameChars):
    with open("characterFile.json") as read_file:
        y = json.load(read_file)
        for x in y:
            gameChars.append(x)

        for z in gameChars:
            print(z.getStats())
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.