mardi 17 janvier 2023

How to Extract Variable from CSV and Save to Another CSV file. Then Cross reference Variable file to see if Variable has already been extracted

I'm importing a random row from a CSV file and then converting that row into a variable. I then want to save that variable to another file (usedWords.csv) and then have the script cross reference the usedWords.csv file to see if the next randomly selected has already been selected.

Code snippet: ` import random import csv

with open('dictionary.csv') as f:
    reader = csv.reader(f)
    chosen_row = random.choice(list(reader))
    print(chosen_row)

with open('usedWords.csv', 'w',newline='') as f:
    #f.write(str(chosen_row))
    chosen_row = ','.join(chosen_row) + '\n'
    for i in chosen_row:
    if chosen_row != i:
         f.write(str(chosen_row))
    else:
        chosen_row == i
        print("Word Already Used")

`

The code I have currently iterates the word, multiple times, and refreshes the csv file each time. The previous word is deleted. The script would need to save the chosen word, start a new line and save then next chosen word each time the program is activated. Newbie here, any help would be appreciated.

Cheers,

Tried iterating. Tried writing to file. Tried Searching Stack Overflow for suggestions. Don't know after that.




Aucun commentaire:

Enregistrer un commentaire