I have the following code but with the input file studentInfo.txt that has the input: ed18, Edward Duran, 077, 3.5  abc1234MikePeterson, 123, 3.7 dw0,Danika Wilson,  083  , 3.9 wd50,WillDavidson, 090,3 hpj4332, Helen Jordan1983.88 xd222XavierDavies1983.65 ah1     Allisson  Henson  147               2.9 ne099 NinaEstrada,     095, 2 sh5555 Skylar Huff 112 3.3 def reformat_student_info(filename):     try:         #Function to split the name into first and last parts         def split_name(name):             parts = name.split()             if len(parts) == 2:                 return parts             else:                 #If no space assume last name starts after first letter                 return [parts[0], parts[1:]]         #Function to format GPA         def format_gpa(gpa):             #Ensure exactly one decimal point             if '.' not in gpa:                 gpa = gpa + '.0'             return gpa         #Open input file         with open(filename, 'r') as input_file:             lines = input_file.readlines() #Open output file for writing         with open('studentInfoReformatted.csv', 'w') as output_file:             for line in lines:                 line = line.strip()  #Remove unnecessary spaces and newlines                 #Split line into netID, name, major, and GPA                 parts = line.split(',')                 if len(parts) == 4:                     netID, name, major, gpa = parts                     netID = netID.strip()                     name = name.strip()                     major = major.strip()                     gpa = gpa.strip()                 #Format the name and GPA                 first_name, last_name = split_name(name)                 gpa = format_gpa(gpa)                 #Write reformatted data to output file                 output_file.write(f"{netID}, {first_name} {last_name}, {major}, {gpa}\n")     except:         raise NotImplementedError() I am getting the output: ed18 Edward Duran 077 3.5 ed18 Edward Duran 077 3.5 dw0 Danika Wilson 083 3.9 wd50 WillDavidson [] 090 3.0 wd50 WillDavidson [] 090 3.0 wd50 WillDavidson [] 090 3.0 wd50 WillDavidson [] 090 3.0 wd50 WillDavidson [] 090 3.0 wd50 WillDavidson [] 090 3.0  Note: the rest of the names and info is not outputting to the studentInfoReformatted.csv

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have the following code but with the input file studentInfo.txt that has the input:

ed18, Edward Duran, 077, 3.5 
abc1234MikePeterson, 123, 3.7
dw0,Danika Wilson,  083  , 3.9
wd50,WillDavidson, 090,3
hpj4332, Helen Jordan1983.88
xd222XavierDavies1983.65
ah1     Allisson  Henson  147               2.9
ne099 NinaEstrada,     095, 2
sh5555 Skylar Huff 112 3.3

def reformat_student_info(filename):
    try:
        #Function to split the name into first and last parts
        def split_name(name):
            parts = name.split()
            if len(parts) == 2:
                return parts
            else:
                #If no space assume last name starts after first letter
                return [parts[0], parts[1:]]

        #Function to format GPA
        def format_gpa(gpa):
            #Ensure exactly one decimal point
            if '.' not in gpa:
                gpa = gpa + '.0'
            return gpa

        #Open input file
        with open(filename, 'r') as input_file:
            lines = input_file.readlines()

#Open output file for writing
        with open('studentInfoReformatted.csv', 'w') as output_file:
            for line in lines:
                line = line.strip()  #Remove unnecessary spaces and newlines

                #Split line into netID, name, major, and GPA
                parts = line.split(',')
                if len(parts) == 4:
                    netID, name, major, gpa = parts
                    netID = netID.strip()
                    name = name.strip()
                    major = major.strip()
                    gpa = gpa.strip()

                #Format the name and GPA
                first_name, last_name = split_name(name)
                gpa = format_gpa(gpa)

                #Write reformatted data to output file
                output_file.write(f"{netID}, {first_name} {last_name}, {major}, {gpa}\n")
    except:
        raise NotImplementedError()

I am getting the output:

ed18 Edward Duran 077 3.5
ed18 Edward Duran 077 3.5
dw0 Danika Wilson 083 3.9
wd50 WillDavidson [] 090 3.0
wd50 WillDavidson [] 090 3.0
wd50 WillDavidson [] 090 3.0
wd50 WillDavidson [] 090 3.0
wd50 WillDavidson [] 090 3.0
wd50 WillDavidson [] 090 3.0 
Note: the rest of the names and info is not outputting to the studentInfoReformatted.csv
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education