Home > Software engineering >  Using Python to build a JSON file containing a list of keywords to be parsed through later
Using Python to build a JSON file containing a list of keywords to be parsed through later

Time:08-21

# List containing keywords to parse data from JSON file.
import json

keywords_list = []


def keyword_load():
    """Loads the JSON file to add keywords to."""
    with open('keywords.json') as kl:
        keywords_list = json.load(kl)
            
def keyword_add():
    """Asks users to add a new keyword."""
    new_word = input("\nAdd a keyword: ")
    keywords_list.append(new_word)

def keyword_show():
    """Shows the list of keywords."""
    print("\n======List of keywords======")
    [print(word) for word in keywords_list]

def keyword_write():
    """Function writes the list of keywords to JSON file."""
    with open('keywords.json', 'a ') as f:
        json.dump(keywords_list, f, indent=2)


#Program Begin...

print("Welcome to the Keyword Adding Module.\n")


keyword_show()

while True:
    keyword_load()
    add_word = input("\nWould you like to add another word? (y/n): ")
    
    if add_word == "y":
        keyword_add()
        keyword_show()
        keyword_write()

    elif add_word == "n":
        # If data entry is complete, write the list contents to JSON file.
        keyword_write()
        close = input("Hit enter to close.")
        break

    else:
        # In case a user responds with neither a 'y' or 'n'.
        print("\nATTENTION: Please enter 'y' or 'n'")

The program asks a user if they would like to add a keyword, upon 'y' input, the user is prompted to add a word... which is then displayed in the list. The user can continue building the list as it is displayed above the prompt. When the user enters 'n' to end adding words, a JSON file is written with the words.

Here's the problem, if the user runs the module again, when the words are added, it adds another JSON object without proper indentation and I get and 'unexpected end of file' error. Additionally, I want all the words to be included in one single JSON object. But in the end, I just want a list of words that can be added to by running this program.

If there's a better way, I'm all ears!

p.s. Python 3.10

CodePudding user response:

You cannot append to a JSON file, you have to overwrite it. JSON is a structured format which does not lend itself to easy amending (though you could have JSON lines format where every line of the file is an entire JSON structure, but then you'd have to write the output on a single line).

Since you are writing out the entire dictionary every time, appending to the previous list would have other problems, too (your deleted words would appear in the earlier versions of the data, and then your program would have to figure out how to only get the latest list when loading it).

The simple fix is to change the open mode from 'a ' to 'w'.

Since your data structure is trivial, an alternative fix would be to use a simple text file with one word per line, and only write out the new word(s). But then you'd still have to rewrite the entire file when you want to delete a word.

While working on your program, you could wreck the file if you have a bug which causes the writing function to fail. Other than that, overwriting the old data should be safe. Maybe take care to save a backup of the file every once in a while, or make sure you only use easily replaceable test data while developing.

  • Related