Apr-06-2022, 07:54 AM
Hi. I'm totally new to python. Just trying to find out how to pick few, but not the same (unique by ID) winners from the .csv file. Anyone could help?
This code picks only one. I guess I need to store those values in a list.
This code picks only one. I guess I need to store those values in a list.
def generate(): global filename, totalEntries, timestamp, winnerName, winnerID, winnerEmail filename = enterFile() now = datetime.datetime.now() timestamp = now.strftime("%B %d, %Y %I:%M:%S %p") with open(filename, newline="") as entriesCSV: entriesDict = csv.reader(entriesCSV,dialect="excel") totalEntries = len(list(entriesDict)) - 1 # ignore our header row winningNumber = random.randint(1,totalEntries) with open(filename, newline="") as entriesCSV: entriesDict = csv.DictReader(entriesCSV,dialect="excel") for row in entriesDict: if int(row["#"]) == winningNumber: winnerName = row["Name"] winnerID = row["ID"] winnerEmail = row["Email"] print(f"The winner is {winnerName}, ID {winnerID}, email {winnerEmail}")And store picked values into one .docx file like this:
def createAuditSheet(): doc = docx.Document() doc.add_paragraph("Giveaway: __________________________________________________________________________________________") doc.add_paragraph("______________________________________________________________________________________________________") doc.add_paragraph("Prize: _______________________________________________________________________________________________") doc.add_paragraph(f"The winner is {winnerName}, ID {winnerID}, email {winnerEmail}.") doc.add_paragraph(f"Drawn {timestamp} by user {currentUser} from {totalEntries} total entries found in file {filename}") for i in range(5): doc.add_paragraph("") doc.add_paragraph("Signature _________________________________________________________________ Date ___________________") doc.add_paragraph("Pick Up Date ___________________") doc.save("GiveawayDrawingResults.docx")