'Python how to save user data to file and then replace specific user data

I don't have specific code for this, im asking you guys to help me. Up to a point i can do it, like saving user data to a txt.

For example users plays a game and in the end their score is saved in a txt like:

user1 15
user2 50
user3 40
user4 90
user5 102

Then how can I search for a user and add score to him?

Like they start a new game and in the end scores will change to:

user1 15 +20 (35)
user2 50 +30 (80)
user3 40 +50 (90)
user4 90 +10 (100)
user5 102 +5 (107)

Thank you!



Solution 1:[1]

I recommend saving the scores in a .csv file where the values (like the name, the score and so on) are separated by a comma “;” or “,”. You can try something like this

from csv import reader
# open file in read mode
with open('students.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Iterate over each row in the csv using reader object
for row in csv_reader:
    # row variable is a list that represents a row in csv
    print(row)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GegiUpTown