I'm fairly new to programming. I'm trying to make a basic chatbot, which can take in new acronym's and their definitions, store them in a dictionary and then write them out to a csv file. I have got this working, however, the second function of the chatbot is to convert the csv file into a dictionary so that it can search for an acronym and return the definition (I also have a working version of this).
Currently the csv file output is all the acronyms and their definitions, but with a blank row in between each entry. Is there a way to stop this happening? (as when I try to read this back in as a dictionary it no longer works)
Or a way to read in a csv file, with these empty rows, to a dictionary?
So in an ideal world I will have a csv file which can be updated with new entries, so in one session a user could add a new entry and then search the updated csv file to check their new entry.
import csv
filename ="input.csv"
acronyms = {}
with open(filename, 'r') as inp:
reader = csv.reader(inp)
acronyms = {rows[0]:rows[1] for rows in reader}
new_acronym = input("What is the acronym you would like to add? ")
new_definition = input("What is the definition of this new acronym? ")
acronyms.update({new_acronym:new_definition})
with open('output.csv','w') as f:
w = csv.writer(f)
w.writerows(acronyms.items())
So this is my working code. At the moment the input csv and output csv are different documents, but as mentioned above, in an ideal world they would be the same document.