pythonjson

python and json files


I am fairly new to python but i know the basics, I am not well versed in .json files and the commands used in python for them. I would like to create a personal app for creating character sheets for D&D and storing the data in json files so you can access it later. I watched the "python tutorial: working with JSON data using the json module" video by corey schafer which did help but I am still getting confused.

this is my code:

#import needed packages
import json 

#inputs
character_name = input("name your character: ")
#character class is not currently being used.
character_class = input("enter character class: ")

#data storage
character_data = {
        'name': character_name,
        'class': character_class
}

with open(character_name, 'w') as f:
    json.dump(character_data, f)


if input("would you like to see your name?") is "yes":
   print()

I would like the name of the character to be the name of a new json file and if the inputed name is a file that already exists it opens that character json file. I assume that would change by just using buttons in frontend dev(html) if thats even how that works. If its a new name a new json file will be created and it will ask for the input data. at the end (very unfinished) I was trying to test to make sure that i could access the json data but got stuck.

anyway if anyone could help I would love any feedback (like if i should even use json) or help with the code. if you think i need to rewrite it let me know and I am up for learning a new language if it would make it that much easier.

edit: I see some of the comments saying it was a broad question which I understand. the response from @mason ritchason was perfect and helped me with everything I needed. if further clarification is needed for some reason let me know :)


Solution

  • Welcome to Stack Overflow!

    You should definitely use and be comfortable with JSON. It is super-duper powerful in programming, computer science, math, etc. JSON is used all over the programming and software world, so a solid understanding will serve you well :)

    Stack Overflow has a ton of information on JSON, and you can also search the web for it to read more. It stands for JavaScript Object Notation; that may help you find more helpful results. The JSON standard is lengthy and boring, but will help you a lot in your learning.

    After you get the character name, you can use os.listdir() to iterate over whatever file directory you are storing your character files in, like so:

    from os import listdir
    
    # returns a list of the filenames (character sheet names) in the folder
    existing_characters = os.listdir(character_sheets_directory)
    

    Then you can check if the input character name is in the folder:

    # if the name is in the list of filenames
    if character_name in existing_characters:
        # open the character sheet, etc.
        ...
    # otherwise the character is new
    else:
        # do new character things
        ...
        # create the .json file and store the info
        character_sheet = open(str(character_name) + ".json", 'x')
        text = json.dumps(character_dict, indent = 4)
        character_sheet.write(text)
        character_sheet.close()
    

    The .JSON file extension will help you organize your files and preserve formatting. Using open with the x operation will create a new file under that name.

    You can use the load, dump, loads and dumps methods in the json library to interact with Python dicts. See the json documentation to learn more about each of these methods. The main difference between the normal and the s methods are that the s methods create a string from the data that can then be written into a file as plaintext. I tend to prefer the loads and dumps methods, and I like to use indentation in my dumps calls:

    File = open(file, 'w')
    text = json.dumps(dict, indent = 4)
    File.write(text)
    File.close()
    

    This will give you a file that's a bit more readable, in this kind of format:

    key:{
        key:value,
        key:{
            key:value
        }
    }
    

    I hope this information helps you achieve your goals!