{"id":433,"date":"2021-03-17T08:47:28","date_gmt":"2021-03-17T08:47:28","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=430"},"modified":"2025-02-14T09:34:01","modified_gmt":"2025-02-14T09:34:01","slug":"read-data-from-file-into-parallel-arrays-2-2","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/read-data-from-file-into-parallel-arrays-2-2\/","title":{"rendered":"Read from file into array of records"},"content":{"rendered":"<h1>Method 1 (SQA)<\/h1>\n<ul>\n<li>This method uses &#8220;arrays&#8221; of a set size and datatype.<\/li>\n<li>The file is explicitly opened and closed<\/li>\n<li>Program will crash if there are not enough data lines in the file. Additional data lines will be ignored.<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom dataclasses import dataclass\r\n\r\nDATA_FILE = &#039;students.csv&#039;\r\nNUMBER_OF_STUDENTS = 4\r\n\r\n@dataclass\r\nclass Student():\r\n    name: str=&#039;&#039;\r\n    mark: int=0\r\n\r\ndef read_data_from_file(): \r\n    # create array of default records\r\n    students = &#x5B;Student() for student in range(NUMBER_OF_STUDENTS)]\r\n \r\n    # open file for reading\r\n    f=open(DATA_FILE, &#039;r&#039;)\r\n \r\n    # read data for every student\r\n    for student in range(NUMBER_OF_STUDENTS):\r\n        # read and decode line from file\r\n        line = f.readline()\r\n        line = line.strip(&#039;n&#039;)\r\n        line = line.split(&#039;,&#039;)\r\n \r\n        # insert data into arrays\r\n        students&#x5B;student].name = line&#x5B;0]\r\n        students&#x5B;student].mark = int(line&#x5B;1])\r\n\r\n    # close data file\r\n    f.close()\r\n     \r\n    return students\r\n \r\n#MAIN PROGRAM\r\nstudents = read_data_from_file()\r\n\r\nfor student in students:\r\n    print(student.name, student.mark)\r\n    \r\n\r\n<\/pre>\n<h1>Method 2 (Pythonic)<\/h1>\n<ul>\n<li>This method appends data onto lists<\/li>\n<li>The file is closed automatically when the with loop completes<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom dataclasses import dataclass\r\n\r\nDATA_FILE = &#039;students.csv&#039;\r\n\r\n@dataclass\r\nclass Student():\r\n    name: str=&#039;&#039;\r\n    mark: int=0\r\n    \r\ndef read_data_from_file():  \r\n    # create empty list\r\n    students = &#x5B;]\r\n  \r\n    # open file for reading\r\n    with open(DATA_FILE, &#039;r&#039;) as f:\r\n        # go through every line of data\r\n        for line in f.readlines():\r\n \r\n            # decode the line \r\n            line = line.strip(&#039;n&#039;)\r\n            line = line.split(&#039;,&#039;)\r\n  \r\n            # create and add record to list\r\n            name = line&#x5B;0]\r\n            mark = int(line&#x5B;1])\r\n            new_student = Student(name, mark)\r\n            students.append(new_student)\r\n  \r\n    return students\r\n  \r\n#MAIN PROGRAM\r\nstudents = read_data_from_file()\r\n\r\nfor student in students:\r\n    print(student.name, student.mark)\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Method 1 (SQA) This method uses &#8220;arrays&#8221; of a set size and datatype. The file is explicitly opened and closed Program will crash if there are not enough data lines in the file. Additional data lines will be ignored. from dataclasses import dataclass DATA_FILE = &#039;students.csv&#039; NUMBER_OF_STUDENTS = 4 @dataclass class Student(): name: str=&#039;&#039; mark: int=0 def read_data_from_file(): # create array of default records students = &#x5B;Student() for student in<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/read-data-from-file-into-parallel-arrays-2-2\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":330,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-433","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=433"}],"version-history":[{"count":6,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/433\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/433\/revisions\/434"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/330"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}