Higher SDD Unit Assessment

  • tliversedge
    Participant

    Just wondered if anyone has any experience of Unit Assessment 3 using Python where candidates have to read the pupil names, coursework mark and prelim mark from an external file. I am assuming they have to read the data from the csv file into 3 separate arrays, one for names, one for coursework mark and one for prelim mark.

    How does Python do this?

    It would be very simple just to split the csv file into 3 separate csv files for the three components and then read the data from the 3 csv files into the 3 arrays. But I am not sure if candidates are expected to read the data from the one csv file into 3 separate arrays.

    Any advice, greatly received!

    pliddle34
    Participant

    Surely a CSV file is only a CSV file if it has multiple items on each line?

    It isn’t that complex in Python. There is an example here:

    http://highercomputingscience.org/wiki/Sequential_files

    It does what you are talking about but one of the items of data is a registration class instead.

    It would be something like:

    filename = “people.csv”
    name = []
    coursework = []
    prelim = []

    #open file
    txtfile = open(filename)

    print “Loading file”,filename

    # for each line in file
    for line in txtfile:
    l = txtfile.readline() # read into line
    mylist = l.split(‘,’) # split into 3 elements
    name.append(mylist[0].strip()) # add element 1 to name array
    coursework.append(mylist[1].strip()) # add element 2 to regclass array
    prelim.append(mylist[2].strip()) # add element 3 to score array
    txtfile.close()

    pliddle34
    Participant

    (But with the tabs that the forum has stripped out of course!)

    Mrs Janet McDonald
    Participant

    You can do it even more simply than that:

    import csv

    array1 = []
    array2 = []
    array3 = []

    myfile = open(“name of your file here”, “r”)
    reader = csv.reader(myfile)

    for row in reader:
    array1.append(row[0])
    array2.append(row[1])
    array3.append(row[2])

    myfile.close()

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.