Skip to content Skip to sidebar Skip to footer

Python - Edit Specific Cell In CSV File

I have created a quiz game in Python that also contains a login and register program however I'm trying to add a scoring system. Currently, I have the CSV file set out like this: u

Solution 1:

TL;DR

if int(score) > int(currentScore):
    currentscore = score

    r = csv.reader(open('users.csv'))
    lines = list(r)
    for n, line in enumerate(lines):
        if [username, password] == list(line):
            break
    lines[n] = username, password, score

    writer = csv.writer(open('users.csv', 'w'))
    writer.writerows(lines)

quiz = False

Explanation: Editing a row will need to be done in two steps since it's not practical to read and write to the same line of the file, so first open the csv file, read and save the data it to a list, edit the list, then open the file again to write the edited list back to the csv.


Post a Comment for "Python - Edit Specific Cell In CSV File"