Separate first, middle and last names (Python)
<p>I have a list of several hundred members that I want to separate by First Name, Middle Name and Last name, but some of the members have prefixes (denoted by 'P'). All possible combinations:</p>

<pre><code>First Middle Last
P First Middle Last
First P Middle Last
P First p Middle Last
</code></pre>

<p>How do I separate First (with P, if available), Middle (with P, if available) and Last names in Python? This is what I came up with but it doesn't quite work.</p>

<pre><code>import csv
inPath = "input.txt"
outPath = "output.txt"

newlist = []

file = open(inPath, 'rU')
if file:
    for line in file:
        member = line.split()
        newlist.append(member)
    file.close()
else:
    print "Error Opening File."

file = open(outPath, 'wb')
if file:
    for i in range(len(newlist)):
        print i, newlist[i][0] # Should get the First Name with Prefix
        print i, newlist[i][1] # Should get the Middle Name with Prefix
        print i, newlist[i][-1]
    file.close()
else:
    print "Error Opening File."
</code></pre>

<p>What I want is:</p>

<ol>
<li>Get first and middles names with their prefixes if available</li>
<li>Output each (first, middle, last) to separate txt files, or a single CSV file (preferable).</li>
</ol>

<p>Many thanks for your help.</p>
