Question

Python Program: Design the logic for and implement a program that merges the two files into...

Python Program: Design the logic for and implement a program that merges the two files into one text file (.txt) containing a list of all students, maintaining ID number order. At the end of the text file provide descriptive data that lists a count of students in each major, the average GPA for each major, and a count and average GPA for the whole program. The data files are already sorted in student ID order. Your program should create a merged file called 'DeptStudentMerge.txt'. Use the default folder as the file location (do NOT list out a path on your own computer or request a path). Here's the program I have so far and the dataset in on the bottom.

CINSMajors

214, Davis, Kimberly, 3.4
315, Thompson, Hunter, 2.9
319, Harris, Michael, 2.3
320, Nichols, Matthew, 3.5
322, Pohler, Geoffrey, 4.0
412, Max, Manfreid, 3.2

PBISMajors

202, Kennedy, Lincoln, 2.4
306, Laslow, Bernard, 2.5
393, Nuance, Deborah, 2.6
395, Samson, Michelle, 3.1

#Declarations
CINSFile = ''
PIBSFile = ''
DeptStudentMergeFile = ''
CINSRecord = []
PIBSRecord = []
count = 0
END_NAME = 'ZZZZZ'
areBothAtEnd = 'N'

def ReadCINS(CINSFile):
global CINSRecord

CINSRecord = CINSfile.readline().rstrip('\n').split(',')

if CINSRecord[0] == '':
CINSRecord[0] = END_NAME

def ReadPIBS(PIBSFile):
global PIBSRecord

PIBSRecord = PIBSfile.readline().rstrip('\n').split(',')

if PIBSRecord[0] == '':
PIBSRecord[0] == END_NAME

def CheckEnd(CINSFile, PIBSFile):
global areBothAtEnd

if CINSRecord[0] == END_NAME:
if PIBSRecord[0] == END_NAME:
areBothAtEnd = 'Y'

def GetReady():
global CINSFile
global PIBSFile
global DeptStudentMergeFile
global areBothAtEnd

CINSFile = open('CINSData.txt', 'r')
PIBSFile = open('PIBSData.txt', 'r')
DeptStudentMergeFile = open('DeptStudentMerge.txt', 'w')


def FinishUp(CINSFile, PIBSFile, DeptStudentMergeFile):
CINSFile.close()
PIBSFile.close()
DeptStudentMergeFile.close()

def MergeRecords(CINSFile, PIBSFile, DeptStudentMergeFile):
global CINSRecord
global PIBSRecord

outputline = float()
CINSRecord = ReadCINS(CINSFile)
PIBSRecord = ReadPIBS(PIBSFile)
areBothAtEnd = CheckEnd(CINSFile, PIBSRecord)
  

if CINSRecord[0] < PIBSRecord[0]:
outputline = CINSRecord[0] + ',' float(CINSRecord[1] + '\n'
CINSFile = ReadCINS(CINSFile)
else:
outputline = PIBSRecord[0] + ',' + float(PIBSRecord[1]) + '\n'
PIBSFile = ReadPIBS(PIBSFile)

DeptStudentMergeFile.write(outputline)
areBothAtEnd = CheckEnd(CINSFile, PIBSFile)


def Main():
#Mainline logic

CINSFile, PIBSFile, DeptStudentMergeFile = GetReady()

while areBothAtEnd != 'Y':
MergeRecords()

FinishUp(CINSFile, PIBSFile, DeptStudentMergeFile)

if __name__== '__main__':
Main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

f1 = open("cins.txt", "r")
f2=open("pibs.txt","r")
f3=open("combine.txt","a")
dict={}
first=0
second=0
firstg=0
secondg=0
for line in f1:
    string = line.split(",")
    first=first+1
    dict[int(string[0])]=",".join(string[1:])
    line=line.replace(" ","").rstrip('\n').split(",")
    firstg=firstg+float(line[len(line)-1])

for line in f2:
    string = line.split(", ")
    second=second+1
    dict[int(string[0])]=",".join(string[1:])
    line=line.replace(" ","").rstrip('\n')
    secondg=secondg+float(line[len(line)-1])
  
for i in sorted(dict.keys()):
    f3.write(str(i))
    f3.write(dict[i])

f3.close()
f1.close()
f2.close()

print("Total number of students in the class are ",first+second)
print("Total number of students in CINS major are ",first)
print("Total number of students in PBIS major are ",second)
print("Average GPA of students in CINS major is ",round(firstg/first,2))
print("Average GPA of students in PBIS major is ",round(secondg/second,2))
print("Average GPA of whole cass is ",round((firstg+secondg)/(first+second),2))

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Python Program: Design the logic for and implement a program that merges the two files into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • Python I'm creating two linked lists from two txt files, merging them, then printing any duplicates...

    Python I'm creating two linked lists from two txt files, merging them, then printing any duplicates found. My program right now only reads numbers one after another following a comma. For example 10,20,15,2. How do i change it so it reads numbers one below another. Like this,                  class Node(object):     value = -1     next = None        def __init__(self,item):         self.val = item         self.next = None def merge_lists(vivendi, activision):     head = tail = Node(0)    ...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • 12.8 GPA reports using files This program is to compute and write to a file the...

    12.8 GPA reports using files This program is to compute and write to a file the GPA for student scores read from an input file. As in Lab 7.5, the point values of the grades are 4 for A, 3 for B, 2 for C, 1 for D, and 0 for F. Except for the grade A, where + has no effect, a + after the letter increases the point value by 0.3, and except for F, where a -...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT