Search code examples
pythonprogramming-languages

I am having a problem with my python program that im running


From an input file I'm suppose to extract only first name of the student and then save the result in a new file called "student-­‐firstname.txt" The output file should contain a list of first names (not include middle name). I was able to get delete of the last name but I'm having problem deleting the middle name any help or suggestion?

the student name in the file look something like this (last name, first name, and middle initial)

Martin, John

Smith, James W.

Brown, Ashley S.

my python code is:

f=open("studentname.txt", 'r')

f2=open ("student-firstname.txt",'w')

str = ''

for line in f.readlines():

     str = str + line

     line=line.strip()

     token=line.split(",")

     f2.write(token[1]+"\n")

f.close()

f2.close()

Solution

  • f=open("studentname.txt", 'r')
    f2=open ("student-firstname.txt",'w')
    
    for line in f.readlines():
         token=line.split()
         f2.write(token[1]+"\n")
    f.close()
    f2.close()