I have a text file containing all the students' names and other information of my programing course like this:
Smith, John sj0012@uni.edu smjo0012@student.edu Student
Lester, Moe mole0025@uni.edu mole0025@student.edu Student
Christ, Jesus jech0020@uni.edu jech@student.edu Student
...
Some of them contains tabs and other unnecessary spaces in between the text within each line. So that the first email address and the second are tabbed. Sometimes between both these and between 'Student'. But my intention is just making a new text file containing just the Name, Lastname in a nice column. I did manage to get my result but only by keep converting the text to list and back to string again. Is there a better way of doing this? Python 2.7
peps = open('ppl.txt', 'r')
for line in peps.readlines():
line = line.strip() # Delete space
line = line.split('\t') # Split at tab indentation and make a list
line = map(lambda s: s.strip(), line) # Remove tab indentation
del line [1:] # Delete everything after Name.
line = ','.join(line) # Make Lastname, Name a string at ','
line = line.split(',') # Make Lastname, Name a list at ','
line[0], line[-1] = line[-1], line[0] # Exchange position of Lastname, Name
line = ', '.join(line) # Convert to string again and join at ','
print line