I'm stuck with my python csv homework

194
April 29, 2017, at 03:11 AM

When I run the code the first time it works. When I run it a second time, it can't find the csv file until I replace it. I don't know why this is.

I would also like to sort the times with the fastest at the top.

    def read_class_data():
    import csv
    driver=[]
    team=[]
    input_file = open("E:\Advanced Higher Computing\Racing.csv","r")
    data = csv.reader(input_file)
    print ("driver: ", driver)
    for eachline in data:
        driver.append(eachline[0])
        team.append(eachline[1])
    input_file.close()
    return driver, team
def register():
    time=[]*6
    for i in range(6):
        temptime = float(input("What is your best time?"))
        while temptime < 0:
            print("Time cannot be less than 0.")
            temptime = float(input("What is your best time?"))
        time.append(temptime)
    return time
def write_class_data(dr, tm, time):
    import csv
    output_file = open("E:\Advanced Higher Computing\Racing.csv","w")
    data = csv.writer(output_file)
    for i in range(len(time)):
        data.writerow([dr[i], tm[i], time[i]])
    output_file.close()
dr, tm = read_class_data()
time = register()
write_class_data(dr, tm, time)
Answer 1

You have many problems in your code, but let's just look at the first function

def read_class_data():
    import csv
    driver=[]*6
    teams=[]*6
    input_file = open("E:\Advanced Higher Computing\Racing.csv","r")
    data = csv.reader(input_file)
    loop = 1
    for eachline in data:
        driver[loop] = eachline[0]
        team[loop] = eachline[1]
        loop = loop + 1
        inputfile.close
    return driver, teams

First,

driver =[]*6

doesn't do what you seem to think it does. It creates an empty array, i.e. one of length zero.

That's actually where you are getting the error. On the first pass through the loop, loop has the value 1 and you are trying to assign to driver[1] ... which doesn't exist, because len(driver) == 0.

Next, setting loop to 1 shows that you are thinking Python list indices are 1-based. They're not: Python uses zero-based indexing.

You need to append to a list to add new data, so your loop might look more like

for eachline in data:
    driver.append(eachline[0])
    team.append(eachline[1])

Next, you have a call to inputfile.close in the loop. You actually aren't calling it because you don't have the needed parentheses. It should be inputfile.close().

But if it was, your loop would crash on the second pass, because the file was already closed on the previous pass. You need to move that line out of the loop.

Finally, you should be using context-managers anyway (i.e. with) so the function ends up something like

def read_class_data():
    import csv
    driver = []
    teams = []
    with open("E:\Advanced Higher Computing\Racing.csv","r") as input_file:
        data = csv.reader(input_file)
        for eachline in data:
            driver.append(eachline[0])
            team.append(eachline[1])
    return driver, teams
READ ALSO
TextInput values cannot be saved on sqlite

TextInput values cannot be saved on sqlite

I'm new to Python and KivyNow, I am working on an application that uses python and I also use kivy for the interface

284
How to control EOL and leading spaces in Mako?

How to control EOL and leading spaces in Mako?

With the following, I would like to control the EOL:

217
How to search for a variable in a list of strings

How to search for a variable in a list of strings

I have very basic python knowledgeThis is my code so far: when i run this code the error UnboundLocalError: local variable 'response' referenced before assignment on line 7 displays

248