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!

    Lee Murray
    Participant

    My class did this Unit Assessment last year and I taught them a number of ways of reading data from files.

    When the data is comma delimited (I used a txt file, not a csv, but I’m sure it’d work the same way), I use the .split(“,”) function. This creates a new list with three items in it (name at index 0, coursework mark at index 1, prelim mark at index 2). I then append the three values in that list to three separate lists.

    The code is something like this (the formatting will be messed up, but you’ll get the idea):

    myFile = open(“Class_Marks.txt”, “r”)
    names = []
    cws = []
    prelims = []
    for line in myFile:
    -tab-splitList = line.strip().split(“,”) #.strip() takes out the new line character at the end
    -tab-names.append(splitList[0])
    -tab-cws.append(splitList[1])
    -tab-prelims.append(splitList[2])

    Hopefully that code works (I typed it from memory so there are likely some mistakes).

    Scott Leiper
    Participant

    When reading from a csv in python, you can fixed loop through the file, and each row is returned as a list object with the values contained in the columns as items, you can then append each item to a different list:

    import CSV

    myFile=open(“csvTestfile.csv”,”rb”)
    reader = csv.reader(myFile)

    names_li=[]
    courseworkMarks_li=[]
    prelimMarks_li=[]

    next(reader) #Skips the first row of the csv containing titles

    for row in reader:
    names_li.append(row[0])
    courseworkMarks_li.append(row[1])
    prelimMarks_li.append(row[2])
    myFile.close()

    John Schofield
    Participant

    Hi
    has anyone done this in VB 2013 ie read and write to and from a text file, where you then split each line read in, into an array. I am having real problems doing this.
    Thanks very much
    kind regards
    john

    Mark Hay
    Participant

    I used the Streamreader class last year with my Highers:
    Imports System.IO

    Using sr As StreamReader = New StreamReader(“U:PupilDetails.txt”, FileMode.Open)

    ‘While there is more text on the next line
    Do While sr.Peek <> -1
    ‘read the next line
    line = sr.ReadLine()
    ‘Split line of text where there is a comma and place in array
    PupilDetails = line.Split(“,”)

    ‘1st element of the split array contains the pupil name
    Names(counter) = PupilDetails(0)
    ‘2nd element of the split array contains the pupil’s house
    Houses(counter) = PupilDetails(1)

    ‘increment the counter
    counter += 1
    Loop
    sr.Close()
    End Using

    Mark Hay
    Participant

    And am using Python this year so rather than use the CSV module i just adapted my VB approach:

    def ReadPupilDetails():
    with open(“PupilDetails.txt”) as readfile:
    line = readfile.readline().rstrip(‘n’)
    while line:
    items = line.split(“,”)

    names.append(items[0])
    houses.append(items[1])

    line = readfile.readline().rstrip(‘n’)

    input(“File read…Press any key to continue.”)
    print(“Names: “,names)
    print(“Houses: “,houses)

    return names,houses

    sturnbull
    Participant

    Hi John
    In VB.NET there is also a Split function. Pass in two parameters, the string to be split, in this case the line from the file and the delimiter. For example, using Filesystem I/O in VB.NET………

    Dim splitFields(3) As String ‘an array to hold each field from each line

    FileOpen(1, filename, OpenMode.Input)
    While Not EOF(1)
    splitFields = Split(LineInput(1), “,”)
    something(i) = splitFields(0)
    anotherthing(i) = splitFields(1)
    somethingelse(i) = splitFields(2)
    i=i+1
    End While

    Cheers

    Bill Sargent
    Participant

    This is a method to do it in VB.Net using Streamreader and the split function. The example involves reading 5 lines of data with four pieces of data per line and displaying them in 4 list boxes.

    Dim objTextfile As New System.IO.StreamReader(“H:temperature.csv”)
    Dim NewLine As String
    Dim temp(3) As String ‘there are 4 pieces of data on each line
    Dim counter As Integer

    For counter = 0 To 4 ‘there are 5 lines of data

    NewLine = objTextfile.ReadLine() ‘this reads a complete line of data
    temp = NewLine.Split(“,”c) ‘this splits the data up and assigns each piece to an element of the temp array. The data splits at each comma. The c indicates a single character being used for the split

    lstTown.Items.Add(temp(0)) ‘ this displays each element in a different list box
    lstJune.Items.Add(temp(1))
    lstJuly.Items.Add(temp(2))
    lstAugust.Items.Add(temp(3))

    Next

    objTextfile.Close()
    objTextfile.Dispose()

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

You must be logged in to reply to this topic.