Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:09:36+00:00 2026-06-07T02:09:36+00:00

This is my first post. I have a function that asks for the number

  • 0

This is my first post. I have a function that asks for the number of students. Then, for each student, first three lines contain the following information: Student ID, name, and number of courses taken that semester. Now, for each course, the course number, credit hour, and the percentage of scores earned by the student is listed.

def rawdata():
    semester = 1
    if semester == 1:
        raw_file = open('raw_data.txt', 'a')
        total = 0.0
        total2 = 0.0
        num_students = int(input('Enter number of students: '))        
        for student in range(num_students):
            raw_file.write('Start' + '\n')
            student_id = input('Enter student ID: ')
            name = input('Enter Name: ')
            num_classes = int(input('Enter number of courses taken: '))
            raw_file.write(student_id + '\n')
            raw_file.write(name + '\n')
            raw_file.write(str(num_classes) + '\n')
            for classes in range(num_classes):
                course_number = input('Enter Course Number: ')
                credits = int(input('Enter Credit Hours: '))
                GPA1 = float(input('Enter percentage grade for class: '))
                raw_file.write(course_number + '\n')
                raw_file.write(str(credits) + '\n')
                raw_file.write(str(GPA1) + '\n')
                total += credits
            raw_file.write('End' + '\n')
        raw_file.close()
        print('Data has been written')

All the data is listed to a txt file and now I need to PULL this information from my raw_data.txt which looks like(varies with inputs):

Start
eh2727
Josh D
2
MAT3000
4
95.0
COM3000
4
90.0
End
Start
ah2718
Mary J
1
ENG3010
4
100.0
End

and process it so that I can calculate each students GPA. I have each students block of info contained by a Start/End and I don’t know how to read this info in my processing function in order for me to calculate their GPA. This is what I have so far:

def process():
    data = open('raw_data.txt', 'r')
    results = open('process_results.txt', 'w')
    buffer = []
    for line in data:
        if line.startswith('Start'):
            buffer = []
        buffer.append(line)
        if line.startswith("End"):
            for outline in buffer:
                results.write(outline)

This simply writes it all into my results text and I don’t know how to individually process each block of information to calculate the GPA. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:09:38+00:00Added an answer on June 7, 2026 at 2:09 am

    Since it is your own code writing out the data to the .txt file, you might consider writing it in an easier and/or more fault tolerant format for machine reading, for example JSON or XML. Alternatively, you might want to consider using pickle or cpickle to serialize the data and read it in again.

    Anyway, on to your question: how to read the file. Unfortunately, you do not tell us what you want to do with the parsed data. I assume here you want to print it. Normally you would of course create a nice class or classes describing students and courses.

    For parsing of files like yours, I use the string method split() a lot. split() is your best friend. See the python docs for more info on string methods.

    f = open('raw_data.txt', 'rt')
    data = f.read()
    
    students = data.split('Start\n')[1:]
    
    for s in students:
        lines = s.split('\n')
        id = lines[0]
        name = lines[1]
    
        nrcourses = int(lines[2])
    
        line = 2
        courses = []
        for n in range(nrcourses):
            number = lines[line+1]
            credit = lines[line+2]
            score = lines[line+3]
            courses.append((number, credit, score))
            line += 3
    
        print 'id: %s; name %s; course list %s' % (id, name, courses)
    
    f.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post. I have this function for reversing a string in
This is my first post and I have am concerned that some might think
This is my first post and I have the feeling that this is something
I have a javascript function that looks like this: function getAlerts() { $.post('getAlerts.php', function(data)
This is my first post here so kindly pardon any mistakes that I have.
This is my first post and I have to admit, I am terrible at
This is my first post here. I have a problem. I need to take
This is my first post and I my first experience with jquery. I have
be kind this is my first post. I have a Crop intent retuning and
I can't understand why this don't work. First i have a function like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.