{"id":430,"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:12:03","modified_gmt":"2025-02-14T09:12:03","slug":"read-data-from-file-into-parallel-arrays-2","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/read-data-from-file-into-parallel-arrays-2\/","title":{"rendered":"Read from file into parallel arrays"},"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<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nDATA_FILE= &#039;students.csv&#039;\r\nNUMBER_OF_STUDENTS = 4\r\n\r\ndef read_data_from_file(): \r\n    # create empty arrays\r\n    names = &#x5B;&quot;&quot;] * NUMBER_OF_STUDENTS\r\n    marks = &#x5B;0] * 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        # decode each 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        names&#x5B;student] = line&#x5B;0]\r\n        marks&#x5B;student] = int(line&#x5B;1])\r\n\r\n    # close data file\r\n    f.close()\r\n\r\nreturn names, marks\r\n\r\n#MAIN PROGRAM\r\nnames, marks= read_data_from_file()\r\n \r\nfor student in range(NUMBER_OF_STUDENTS):\r\n    print(names&#x5B;student], marks&#x5B;student])\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\n# define filename as a constant\r\nDATA_FILE= &#039;students.csv&#039;\r\n\r\ndef read_data_from_file():\r\n    # create empty lists\r\n    names = &#x5B;]\r\n    marks = &#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            # add data to lists\r\n            names.append(line&#x5B;0])\r\n            marks.append(int(line&#x5B;1]))\r\n\r\n    return names, marks\r\n\r\n#MAIN PROGRAM\r\nnames, marks = read_data_from_file()\r\n\r\nfor student in range(len(names)):\r\n    print(names&#x5B;student], marks&#x5B;student])\r\n<\/pre>\n<pre><\/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 DATA_FILE= &#039;students.csv&#039; NUMBER_OF_STUDENTS = 4 def read_data_from_file(): # create empty arrays names = &#x5B;&quot;&quot;] * NUMBER_OF_STUDENTS marks = &#x5B;0] * NUMBER_OF_STUDENTS # open file for reading f=open(DATA_FILE, &#039;r&#039;) # read data for every 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\/\">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-430","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/430","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=430"}],"version-history":[{"count":12,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/430\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/430\/revisions\/522"}],"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=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}