Python reading in a file and parsing it to variables with whitespaces
<p>I have a text file (Students.txt) i need to read into python and parse them into variables first_name, middle_name, last_name, student_id. The first few lines of the text file is as shows: </p>

<pre><code>Last Name  Midle Name  First Name   Student ID  
----------------------------------------------
Howard     Joe          Moe         howar1m     
Howard                  Curly       howar1c     
Fine       Ken          Lary        fine1l   
</code></pre>

<p>The code I've tried </p>

<pre><code>f = open("Students.txt")
for line in f:
    fields = line.strip().split()
    last_name = fields[0]
</code></pre>

<p>Only works for last name, because if I try <code>fields[1]</code> for middle name, I get a "list index out of range" error. I've tried using <code>if not line.startswith('Middle Name'): continue</code>  but it doesn't recognize the column. 
             Does anyone have a better approach as to how to parse these into their respective variables?</p>

<p>Probably you should use readlines(), and then loop over each line with skipping first 2 rows. Hence the code will look like</p>

<pre><code>f = open("Student.txt")
lines = f.readlines()
print lines
for line in lines[2:]:
    fields = line.split()
    last_name = fields[0]
    middle_name = fields[1]
</code></pre>

python parsing