Python Forum
Pick random winners from .csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pick random winners from .csv
#1
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.

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")
Reply


Messages In This Thread
Pick random winners from .csv - by DellXT - Apr-06-2022, 07:54 AM
RE: Pick random winners from .csv - by deanhystad - Apr-06-2022, 02:04 PM
RE: Pick random winners from .csv - by DellXT - Apr-07-2022, 07:31 AM
RE: Pick random winners from .csv - by Larz60+ - Apr-07-2022, 09:34 AM
RE: Pick random winners from .csv - by deanhystad - Apr-07-2022, 07:39 PM
RE: Pick random winners from .csv - by DellXT - Apr-08-2022, 10:20 AM
RE: Pick random winners from .csv - by deanhystad - Apr-08-2022, 12:48 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020