Python Searching for variable in file and appending another value

JJA

I'm making a quiz which requires to import the users name and score to the text file. I then check if the users score has already been submitted in the quiz so I can append the score to that name instead of creating a new set of data. This needs to be able to append THREE scores and then to remove the first score, second=first, third=second, score=third.

if classs == "1":
    text_file = open("Class1.txt", "r")
    data = text_file.read()
    text_file.close()
    if name in data:
        combdata = (name,score)
else:
    text_file = open("Class1.txt","a")
    text_file.write(str(name) +","+ str(score)+"\n")
    text_file.close()

Here is the code trying to search the file and append the score of the user to that name set.

liste = []
liste.append(data)
    for line in data:
        for name in line:
            liste.append(score)

print(liste)
break

However, the score is appended like this

['Dion,0\nJarrod,1\nJake Bing,6\nFat,0', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
toti08

When you append something to a list it is added at the end of that list. So in your case you need to re-write your file in order to append the new score to the line you want. So I suppose you would need a function that searches for a certain name in the file and adds the new score in a new file:

def searchName(name, score):
    with open(fileName, 'r') as f:
        data = f.read().split('\n')
        with open(newFileName, 'w') as newFile:
            for line in data:
                if name in line:        
                    line+=(',') + str(score) + (',')
                newFile.write(line + '\n')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Appending variable list to list in Python

From Dev

Python - searching if string is in file

From Dev

Python: Using a returned value to reference another variable?

From Dev

Scrapy appending Key: value pairs to another Key

From Dev

Appending JSON data from one file to another in Python

From Dev

Searching for a value in a variable... Javascript

From Dev

Python appending file remotely

From Dev

Appending a value to a key in Python?

From Dev

Appending a 0 in front of a variable in Python

From Dev

Change value of a variable depending on another in python

From Dev

Appending values to a Python member variable

From Dev

Searching for a value in a file with two delimiters

From Dev

Python file not appending on Raspberry Pi

From Dev

how to call a variable value in another php file

From Dev

Python file searching trouble

From Dev

Variable from another file through dict value in python

From Dev

Scrapy appending Key: value pairs to another Key

From Dev

Why does the value of a variable changes on appending to python list as a list?

From Dev

Appending JSON data from one file to another in Python

From Dev

Appending a string to a file with variable content

From Dev

Appending a new value to python list

From Dev

Python Assigning value from one variable to another

From Dev

Appending variable to text file not working in Python

From Dev

searching and appending to line in python

From Dev

Searching through a file in Python

From Dev

File iteration and variable searching

From Dev

Appending a tuple into another tuple python

From Dev

Python list not appending & variable not defined

From Dev

Appending to a file error - PYTHON

Related Related

HotTag

Archive