Task: Design of import class
Completion requirements
Import of csv file
Task:
Opened: Thursday, 3 October 2019, 12:00 AM
Due: Thursday, 10 October 2019, 12:00 AM
Reading of students.csv
The sample structure of this CSV file is as follows:
surname;first name;date of birth;school classThe pure reading of the CSV file students.csv is quickly done.
Colligan;Manda;22.09.2004;c1
Hickle;Sara;31.01.2003;c2
Murchison;Jeffrey;01.09.2002;c2
Thieme;Madelaine;02.12.2000;c1
Tarleton;Vito;22.02.2004;c2
1 2 3 4 5 6 7 | import os from csv import reader script_dir = os.path.dirname(__file__) # 'os' for compatibility reason abs_file_path = os.path.join(script_dir, rel_path) with open(abs_file_path, newline='') as csvFile: self.__listStudents = list(reader(csvFile, delimiter=';'))[1:] # omit 1st line |
(The library 'os' is used to ensure compatibility between operating systems on file operations. The 1st line has to be omitted.)
Output
[['Colligan', 'Manda', '22.09.2004', 'c1'],Access to the list elements is as before:
['Hickle', 'Sara', '31.01.2003', 'c2'],
['Murchison', 'Jeffrey', '01.09.2002', 'c2'],
['Thieme', 'Madelaine', '02.12.2000', 'c1'],
['Tarleton', 'Vito', '22.02.2004', 'c2']]
for listStudent in listStudents:
print(listStudent[0],listStudent[1],listStudent[2],listStudent[3])
Output
Colligan Manda 22.09.2004 c1
Hickle Sara 31.01.2003 c2
Murchison Jeffrey 01.09.2002 c2
Thieme Madelaine 02.12.2000 c1
Tarleton Vito 22.02.2004 c2
Further informations:
CSV import: https://stackoverflow.com/questions/53571881/read-the-second-row-from-csv-file-to-python
CSV import: https://stackoverflow.com/questions/53571881/read-the-second-row-from-csv-file-to-python
with statement: https://effbot.org/zone/python-with-statement.htm
Task:
Discuss and design in your group the class structures appropriate for a CSV import of students. Sketch a class diagram for a brief introduction to your considerations.