Python - creating multiple objects for a class
<p>In python I need to create 43 instances of a class 'Student' that includes the variables first_name, middle_name, last_name, student_id by reading in a file (Students.txt) and parsing it. The text file appears like this: </p>

<pre><code>Last Name  Midle Name  First Name   Student ID  
----------------------------------------------
Howard                  Moe         howar1m     
Howard                  Curly       howar1c     
Fine                    Lary        fine1l      
Howard                  Shemp       howar1s     
Besser                  Joe         besse1j     
DeRita      Joe         Curly       derit1cj    
Tiure       Desilijic   Jaba        tiure1jd    
Tharen                  Bria        thare1b     
Tai         Besadii     Durga       tai1db      
Hego                    Damask      hego1d      
Lannister               Tyrion      lanni1t     
Stark                   Arya        stark1a     
Clegane                 Sandor      clega1s     
Targaryen               Daenerys    targa1d     
Bombadil                Tom         bomba1t     
Brandybuck              Meriadoc    brand1m     
Took                    Pregrin     took1p      
McCoy                   Leonard     mccoy1l     
Scott                   Montgomery  scott1m     
Crusher                 Wesley      crush1w     
Montoya                 Inigo       monto1i     
Rugen                   Tyrone      rugen1t     
Solo                    Han         solo1h      
Corey                   Carl        corey1c     
Flaumel                 Evelyn      flaum1e     
Taltos                  Vlad        talto1v     
e'Drien                 Morrolan    edrie1m     
Watson                  John        watso1j     
McCoy                   Ebenezar    mccoy1e     
Carpenter               Molly       carpe1m     
Graystone               Zoe         grays1z
Adama                   William     adama1w
Adama       Joseph      Leland      adama1l
Roslin                  Laura       rosli1l
Baltar                  Gaius       balta1g
Tigh                    Ellen       tigh1e
Tigh                    Saul        tigh1s
Cottle                  Sherman     cottl1s
Zarek                   Thomas      zarek1t
Murphy      James       Alexander   murph1a
Sobchak                 Walter      sobch1w
Dane                    Alexander   dane1a
Gruber                  Hans        grube1h
Biggs       John        Gil         biggs1gj
</code></pre>

<p>The class student is:</p>

<pre><code>class Student (object):
    def __init__(self, first_name, middle_name, last_name, student_id):
        self.__first_name = first_name
        self.__middle_name = middle_name
        self.__last_name = last_name
        self.__student_id = student_id
</code></pre>

<p>What would be the easiest way to read into 'Students.txt' and create each instance of student?</p>

<h2>Step by step tutorial</h2>

<p>To read the file content, use <code>io.open</code>. Don't forget to specify the file encoding if any name has accentuated characters.</p>

<pre><code>with io.open('students.txt', mode="r", encoding="utf8") as fd:
    content = fd.read()
</code></pre>

<p>Here, you read the whole content and store it in memory (amount of data is small). You can also use an iterator.</p>

<p>Then, you can split the content line by line with <code>str.splitlines()</code>:</p>

<pre><code>lines = content.splitlines()
# print(lines)
</code></pre>

<p>You get something like:</p>

<pre><code>['Last Name  Midle Name  First Name   Student ID  ',
 '----------------------------------------------',
 'Howard                  Moe         howar1m     ',
 'Howard                  Curly       howar1c     ',
 'Fine                    Lary        fine1l      ',
 'Howard                  Shemp       howar1s     ',
 'Besser                  Joe         besse1j     ',
 'DeRita      Joe         Curly       derit1cj    ',
 'Tiure       Desilijic   Jaba        tiure1jd    ',
 'Tharen                  Bria        thare1b     ']
</code></pre>

<p>You have (nearly) fixed-length lines, so you can use slices to extract the fields.</p>

<p>Here is what you can do for the header:</p>

<pre><code>header = lines.pop(0)
fields = header[0:8], header[11:21], header[23:33], header[36:46]
# print(fields)
</code></pre>

<p>You get:</p>

<pre><code>('Last Nam', 'Midle Name', 'First Name', 'Student ID')
</code></pre>

<p>You can drop the line of hyphens:</p>

<pre><code>lines.pop(0)
</code></pre>

<p>For each line, you can extract values using slices too. Note: slice indices are slightly different:</p>

<pre><code>for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    # print(record)
</code></pre>

<p>You'll get values with trailing space:</p>

<pre><code>('Howard  ', '         ', ' Moe       ', 'howar1m   ')
('Howard  ', '         ', ' Curly     ', 'howar1c   ')
('Fine    ', '         ', ' Lary      ', 'fine1l    ')
('Howard  ', '         ', ' Shemp     ', 'howar1s   ')
('Besser  ', '         ', ' Joe       ', 'besse1j   ')
('DeRita  ', 'Joe      ', ' Curly     ', 'derit1cj  ')
('Tiure   ', 'Desilijic', ' Jaba      ', 'tiure1jd  ')
('Tharen  ', '         ', ' Bria      ', 'thare1b   ')
</code></pre>

<p>To avoid trailing spaces, use <code>str.strip()</code> function:</p>

<pre><code>for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    # print(record)
</code></pre>

<p>You get:</p>

<pre><code>['Howard', '', 'Moe', 'howar1m']
['Howard', '', 'Curly', 'howar1c']
['Fine', '', 'Lary', 'fine1l']
['Howard', '', 'Shemp', 'howar1s']
['Besser', '', 'Joe', 'besse1j']
['DeRita', 'Joe', 'Curly', 'derit1cj']
['Tiure', 'Desilijic', 'Jaba', 'tiure1jd']
['Tharen', '', 'Bria', 'thare1b']
</code></pre>

<p>At this point, I recommend you to store your record as a <code>dict</code> in a list:</p>

<pre><code>records = []
for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    records.append(dict(zip(header, record)))
</code></pre>

<p>You get:</p>

<pre><code>[{'First Name': 'Moe', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1m'},
 {'First Name': 'Curly', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1c'},
 {'First Name': 'Lary', 'Last Nam': 'Fine', 'Midle Name': '', 'Student ID': 'fine1l'},
 {'First Name': 'Shemp', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1s'},
 {'First Name': 'Joe', 'Last Nam': 'Besser', 'Midle Name': '', 'Student ID': 'besse1j'},
 {'First Name': 'Curly', 'Last Nam': 'DeRita', 'Midle Name': 'Joe', 'Student ID': 'derit1cj'},
 {'First Name': 'Jaba', 'Last Nam': 'Tiure', 'Midle Name': 'Desilijic', 'Student ID': 'tiure1jd'},
 {'First Name': 'Bria', 'Last Nam': 'Tharen', 'Midle Name': '', 'Student ID': 'thare1b'}]
</code></pre>

<p>But you can also use a class:</p>

<pre><code>class Student(object):
    def __init__(self, first_name, middle_name, last_name, student_id):
        self.first_name = first_name
        self.middle_name = middle_name
        self.last_name = last_name
        self.student_id = student_id

    def __repr__(self):
        fmt = "&lt;Student('{first_name}', '{middle_name}', '{last_name}', '{student_id}')&gt;"
        return fmt.format(first_name=self.first_name, middle_name=self.middle_name, last_name=self.last_name, student_id=self.student_id)
</code></pre>

<p>And construct a list of students:</p>

<pre><code>students = []
for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    students.append(Student(*record))
</code></pre>

<p>You get:</p>

<pre><code>[&lt;Student('Howard', '', 'Moe', 'howar1m')&gt;,
 &lt;Student('Howard', '', 'Curly', 'howar1c')&gt;,
 &lt;Student('Fine', '', 'Lary', 'fine1l')&gt;,
 &lt;Student('Howard', '', 'Shemp', 'howar1s')&gt;,
 &lt;Student('Besser', '', 'Joe', 'besse1j')&gt;,
 &lt;Student('DeRita', 'Joe', 'Curly', 'derit1cj')&gt;,
 &lt;Student('Tiure', 'Desilijic', 'Jaba', 'tiure1jd')&gt;,
 &lt;Student('Tharen', '', 'Bria', 'thare1b')&gt;]
</code></pre>

python class object object-oriented-analysis