0

I am banging my head agaisnt a wall trying to figure out something that is simple.

Basically I have a .CSV with names in and test scores e.g.

Brad 4, 5, 7, 7
Dan 3, 6, 2, 7

What I want to do is write code that first all of prints out the tests scores. This bit works fine.

The aspect that I can not get to work is the part were the program reads the names, in the CSV. If the name is present it will append the CSV with the new score at the start. So insert the new value at array position 1.

If the name is not present in the CSV it will add name and then again insert the value at array position 1.

Here is the code that does not work currently, I don't believe it to be complicated however I must be thinking about it wrong.

import csv
def names():
    global fn
    fn = input("please enter first name \n").title()
    namecheck = False
    while namecheck == False:
        nc = input("you have entered " + fn + " are you sure \n 1) Yes \n 2) No")
        if nc == "1":
            quiz()
            namecheck = True
        if nc =="2":
            names()

def quiz():
    option = input("do you want to print or append? \n 1) Print 2) Append")
    if option =="1":
        f = open('namelist.csv', 'r')
        a = f.read()
        print(a)
    if option =="2":
        score = input("please enter score")
        score = int(score)
        with open('namelist.csv', 'rt') as f:
            reader = csv.reader(f, delimiter=',')
            for row in reader:
                for field in row:
                    if field == fn:
                        XXXXXXXX <--- this is were I think I am going wrong.
names()
2
  • Just to be sure: Is you original code indented like here in your question? Commented Oct 23, 2015 at 11:58
  • Are you sure you don't want to have a comma after the name in your CSV file like Brad, 2, 5 etc.,? because having it like that makes it easier to update Commented Oct 23, 2015 at 12:05

2 Answers 2

0

when you declare a def, you have to indent the entire section.

def quiz():
[-->INDENT HERE!] option = input("do you want to print or append? \n 1) Print 2) Append")
[-->INDENT HERE!] rest of def..

#back to main statements..

(Of course I don't mean for you to type "[-->INDENT HERE!]" literally)

Sign up to request clarification or add additional context in comments.

1 Comment

Well, but this seems not to be the problem here. Isn't it a newbie problem with formatting the question here on SO?
0

Do you have to literally write to the file every time someone adds a new score or a new name? If writing to file at the end of program is an option, I'd suggest collecting all info ahead of time and then writing the data in bulk toward the end..that way you could maintain everything in memory and do the I/O with file in one go, sparing you a lot of file operations and giving you the convenience of working with python data structures such as dicts/sets etc....

If that's not an option... Looking up names by reading the file is really inefficient as you are reading the entire file just to see if the name is present.

I'd recommend using a dict to store the list of names you've already entered. This way checking the dict to see if the name is present or not is much more efficient...You could try storing the dict with key=name and value=line number on which you entered the name while you wrote to csv.

That way if you found the name in dict, you can go to the particular line and then append your data like this:

Start reading and writing on specific line on CSV with Python

To insert an element in the first position (i am assuming this is after the name), you could do something like:

l = line.split(",")
#assumes first element is the name of the person 
l.insert(1, new_score) # 1 is the position you want to insert the data
#then insert this data into a_NEW_CSV file, as writing to same CSV at same line is difficult...

See this for more details: Replace data in csv file using python

3 Comments

Hey JTurk thanks for the help, yeah I literally have to add one number at a time. So it would do pretty much what you said. Ask for a name if its in the file add it to the row in position 1. If the name is not in the file add the name on a new row at position 0 and the new score at position 1. Its driving me mad! (clearly you can tell coding is not my strong point) :)
so you can't "pool" the updates and write them in bulk toward the end? example: user1 puts in a number, user2 puts in another number and so on..and you store them in a dict form.. {user1:[1,2,3],user2:[4,5,6]} and then toward the end of the program, bulk write this dict into the csv? such as for k,v in dict.iteritems(): write each item to csv?..thats much cleaner if its an option....
Unfortunately no, I am not able to pool them. Only because the course literature states it has to be one at a time, which seems pretty crappy. Want I am doing is creating a test program where users select multiple choice questions. They are then asked 10 questions based on the topic. Their new score is then added after their name into the csv file, unless their name is not present in the csv then it adds the name and the score. Its driving me mad :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.