Skip to main content
eick-at moodle
  • Home
  • Calendar
  • More
You are currently using guest access
Log in
eick-at moodle
Home Calendar
Expand all Collapse all
  1. Programming AD with Python
  2. Import from CSV file
  3. Task: Design of import class

Task: Design of import class

Completion requirements
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 class
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
The pure reading of the CSV file students.csv is quickly done.

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
Import of csv file


(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'], 
['Hickle', 'Sara', '31.01.2003', 'c2'],
['Murchison', 'Jeffrey', '01.09.2002', 'c2'],
['Thieme', 'Madelaine', '02.12.2000', 'c1'],
['Tarleton', 'Vito', '22.02.2004', 'c2']]
Access to the list elements is as before:
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
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.

You are currently using guest access (Log in)
Data retention summary
Get the mobile app
Powered by Moodle