0

Here is my code:

import csv

with open("Grades.txt", "r") as file:
        reader = csv.reader(file)
        for row in reader:
            if name == row[0]:
                with open("Grades.txt", "a") as file:
                    writer = csv.writer(file)
                    writer.writerow(grade)

The variable name and grade have already been defined in an earlier function. I have a text file with a list of names so the code checks if the name(John) is in the text file and then is supposed to write the grade(A) next the name with a comma separating it. The problem is that my code will write the grade a space or 2 spaces below the entire list of names. If I can get it to write to the end of the name it would just be shown like (JohnA) with no separation. Im clueless about how to go about fixing this. I would appreciate if you could correct my code to do what I need it to. The variable name is an input from a login in a different function so the input is different every time. Also new names may be added through my sign up function so the similar question doesn't help.

for example say my text file looked like this:

John

Sam

Bob

And the grade Sam got was an A. How would I append the A grade to the end of Bobs name with a comma separating the name and the grade?

bastelflp
  • 9,362
  • 7
  • 32
  • 67
  • 1
    Possible duplicate of [Inserting Line at Specified Position of a Text File](https://stackoverflow.com/questions/1325905/inserting-line-at-specified-position-of-a-text-file) – Colin Schoen Nov 13 '17 at 19:48

1 Answers1

0

I don't see how this code example should do the job you describe. Sorry.

import csv

students = [["Anne", "A"], ["Emily", "B"]]

with open("grades.csv", "w", newline="") as f:
    writer = csv.writer(f)
    for row in students:
        writer.writerow(row)

You must give a tupel or a list as a row to csv.writer. What you describe sounds that you write two times to that file, but I don't see that this is been done by your code as described.

I hope to help you a little bit. Sorry at the moment I can't comment...

New:

What I want to say is, that you should put the names and grades in your main program together and then write it to the file. This is how I would solve your task.

names = ["John", "Sam", "Bob"]
grades = ["A", "B", "C"]

names_grades = zip(names, grades)

for row in names_grades:
    print(row)

The new row can be written easily to your file.

M14
  • 444
  • 6
  • 8
  • Thanks but your code is writing onto a new line. My code need to append the grade the person receives after taking my quiz and it works it just doesn't append to the row with the variable 'name'. Im not sure how i could explain it better sorry – Felix Wheeler Nov 13 '17 at 20:14
  • This helps! It makes me clearer what you want. Why don't you write to a new file??? This would make your problem very, very easy. I will show you, what I mean. – M14 Nov 13 '17 at 20:33
  • Yes that should work however how do i add to that because somehow i want to keep a record of all the previous grades as well as the new one. Would if you did something like row + grade to re-write the existing row with existing grades but then adding the new grade to the end?? – Felix Wheeler Nov 13 '17 at 20:51
  • If I understand your question: Write to a new file and you have your name-file, your grade-file and your new name-grade-file. I would decide what I would do, when I know your data. Without seeing some dummy-data from you I can't decide. zip with two lists is very quick. – M14 Nov 13 '17 at 21:05