Home > database >  Python. Code leaves blank rows in csv file after every row
Python. Code leaves blank rows in csv file after every row

Time:04-08

Why code deletes not only winningRows, but also csv header row and leaves blank row after every row? How to fix it? I only need to delete winningRows, no blank rows needed.

def generate():
    global winningRows
    filename = enterFile()

    noOfWinners = 5
    winningNumbers = []
    while len(winningNumbers) < noOfWinners:
        luckyNumber = random.randint(1, totalEntries)
        if luckyNumber not in winningNumbers:
            winningNumbers.append(luckyNumber)

    with open(filename, newline="") as entriesCSV:
        entriesDict = csv.DictReader(entriesCSV,dialect="excel")
        allRows = [row for row in entriesDict]
        winningRows = [row for row in allRows if int(row["#"]) in winningNumbers]
        nonWinningRows = [row for row in allRows if int(row["#"]) not in winningNumbers]
        for row in winningRows:
            winnerName = row["Name"]
            winnerID = row["ID"]
            winnerEmail = row["Email"]
            print(f"The winner is {winnerName}, ID {winnerID}, email {winnerEmail}")

        csv.DictWriter(open(filename, "w"), fieldnames=["#", "Name", "ID", "Email"]).writeheader()
        csv.DictWriter(open(filename, "w"), fieldnames=["#", "Name", "ID", "Email"]).writerows(nonWinningRows)

csv before running the code

csv after running the code

It seems this is working:

with open(filename, newline='\n') as entriesCSV:
    entriesDict = csv.DictReader(entriesCSV,dialect="excel")
    allRows = [row for row in entriesDict]
    winningRows = [row for row in allRows if int(row["#"]) in winningNumbers]
    nonWinningRows = [row for row in allRows if int(row["#"]) not in winningNumbers]
    for row in winningRows:
        winnerName = row["Name"]
        winnerID = row["ID"]
        winnerEmail = row["Email"]
        print(f"The winner is {winnerName}, ID {winnerID}, email {winnerEmail}")

with open(filename, "w", newline='\n') as entriesCSV:
    writer = csv.DictWriter(entriesCSV, fieldnames=["#", "Name", "ID", "Email"])
    writer.writeheader()
    writer.writerows(nonWinningRows)

But in this case, I am opening the file twice, right?

CodePudding user response:

Looks like you are on Windows. Add the newline argument to your open function.

with open(filename, "w", newline='\n') as fp:
    writer = csv.DictWriter(fp, fieldnames=["#", "Name", "ID", "Email"])
    writer.writeheader()
    writer.writerows(nonWinningRows)

CodePudding user response:

You are opening the same file multiple times in parallel. I can't test this without the data, but does this work?

with open(filename, newline="") as entriesCSV:
    entriesDict = csv.DictReader(entriesCSV,dialect="excel")

allRows = [row for row in entriesDict]
winningRows = [row for row in allRows if int(row["#"]) in winningNumbers]
nonWinningRows = [row for row in allRows if int(row["#"]) not in winningNumbers]
for row in winningRows:
    winnerName = row["Name"]
    winnerID = row["ID"]
    winnerEmail = row["Email"]
    print(f"The winner is {winnerName}, ID {winnerID}, email {winnerEmail}")

with open(filename, "w") as outputCSV:
    writer = csv.DictWriter(outputCSV, fieldnames=["#", "Name", "ID", "Email"])
    writer.writeheader()
    writer.writerows(nonWinningRows)
  • Related