0

I have a .txt file that is being written to by a python script.

Adam,3,2,4

Sorin,3,2,4

Sorin,0,0,0

new_record = studentName+","+str(Score1) +","+str(Score2) +","+str(Score3)
student_class = 0

while student_class != 1 or student_class != 2 or student_class != 3:
    student_class=input("What class are you in?(1/2/3): ")
    if student_class == "1":
        file=open("Class1.txt", "a+")
        file.write(new_record)
        file.write("\n")
        file.close()
        with open("Class1.txt", 'r') as fp:
            for count, line in enumerate(fp):
                pass
            break

I want the scores to be overwritten if the student name is the same. For example if I run the script again, and Sorin gets a score of "3,3,3" the .txt file would look like this:

Adam,3,2,4

Sorin,3,2,4

Sorin,0,0,0

Sorin 3,3,3

However I want it to turn out like this:

Adam,3,2,4

Sorin 3,3,3

niub
  • 19
  • 4
  • 1
    Your `.txt` format looks like csv, you might want to have a look at the [`csv` module](https://docs.python.org/3/library/csv.html) before anything else. – ljmc Oct 01 '22 at 13:51
  • 2
    You'll need to write some code to figure out if a student name already exists in the file. "Flat" files are not ideal for this. You should probably be using a database or a serialised (pickled) Python dictionary – DarkKnight Oct 01 '22 at 13:53
  • Maybe this might help: [https://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python](https://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python) – ss3387 Oct 01 '22 at 13:59
  • Maybe try using a dictionary (which has the features you're looking for) and using pandas or csv to save this data to a csv file as @Ijmc said. If that's not what you hope to do, you can always use a function to format a dictionary into a text file as you want and another to format a string from a txt file back into a dictionary. – user17301834 Oct 01 '22 at 14:31

1 Answers1

0

There are some things missing in your code, like how we know which student we are working on, etc.

But either way, this is the approach I would take if the files you are working on are not too big, as the file contents will be stored in memory while editing.

It uses a StringIO as intermediary location where the rows are appended, except if the name matches the current student, and then the content of the StringIO as put in place of the original file.

Starting with:

Adam,3,2,4
Sorin,3,2,4
Sorin,0,0,0

And running the following

import csv
from io import StringIO

current_student = "Sorin"
current_scores = (3, 3, 3)

# obtain a temporary file-like object in memory with a csv writer
with StringIO() as f_tmp:
    writer = csv.writer(f_tmp)

    # open the input file for reading with a csv reader
    with open("/tmp/classes.csv", "r", newline="") as f_in:
        reader = csv.reader(f_in)
        for row in reader:
            # skip the rows of current_student
            if row[0] == current_student:
                continue
            writer.writerow(row)
        # add current_student and their scores
        writer.writerow((current_student,) + current_scores)

    # open the input file for writing
    with open("/tmp/classes.csv", "w") as f_out:
        f_out.write(f_tmp.getvalue())

You get

Adam,3,2,4
Sorin,3,3,3
ljmc
  • 4,830
  • 2
  • 7
  • 26