Essentially I am using a python library to query for synonyms of a word, and then adding that word, along with its synonyms to a postgreSQL table. It is fine if some of the table columns are empty, but I do not know how to insert the synonyms to the database because I don't know how many there will be.
E.g I have a 6 column table, 1 for the original word, 5 for its synonyms. For one word I may get 3 synonyms, but for others I might get all 5.
The only way I've managed to program this task is a horrible group of if statements as so:
for word in textlist:
syns = dictionary.synonym(word)
num = len(syns)
if len(syns) == 5:
for i in syns:
syn1 = i[0]
syn2 = i[1]
syn3 = i[2]
syn4 = i[3]
syn5 = i[4]
And so on... and then use those variables to insert the synonyms into the database table.
One way I was thinking of was to build another list of the word and synonyms and loop through that to add to the table. But I have no idea how to go about that. Any help on how to push further would be a big help. Thank you.
Note: syns returns a list object.
EDIT:
So thanks to @systemjack, I've changed the system in such a way that there are 3 table columns. id (which is a serial primary key), word and synos (originally syn, but changed just to make sure it isn't some weird keyword). Each word should be added in a row along with one synonym and then the next row could be the same word with a different synonym and so on...Anyway the new method is as follows:
for word in textlist:
syns = dictionary.synonym(word)
if syns is not None:
for syn in syns:
cursor.execute('INSERT INTO wordsyn (word, "synos") VALUES (%s, %s);', (word, syn))
I now however get the error
psycopg2.ProgrammingError: column "synos" of relation "wordsyn" does not exist LINE 1: INSERT INTO wordsyn (word, "synos") VALUES ('&c
I've put double quotes around synos in an attempt to make it work, but it doesn't work without them either. Any further help would be greatly appreciated.
\d+ wordsyn outputs:
FINAL EDIT: Found the problem. Didn't have the table in the correct db. I thought \dt only showed tables in your current database, but apparently not.