We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,348 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

lab assignment HELP!!

Write a Python program which will read data for several students from a text file and create a list of lists to store that data. The data must be read from a text file named “Lab11.txt”. Each line in the text file contains the first name, last name, techid, number of credits earned, and number of quality points earned for a single student. The individual items of data on a line are separated by one or more spaces. You can see sample data in the D2L file “Lab11.txt”.

Lab11.txt looks like:
Max Medium 12345678 58 152
Jane Johnson 87654321 78 201
Bill Bupkiss 23456789 29 29
Nate Newby 98765432 0 0
Harold Humphries 11223344 43 160
Carol Cramer 22334455 102 400
Alvin Adams 33445566 67 120
Fred Frederick 44556677 81 250
Phillip Parker 55667788 44 168
Sam Spade 24681357 16 30

For each line that you read in, you are to split it apart and create a list of the individual items. You should also convert the number of credits and number of quality points to integers within this list. These individual lists are then to be concatenated into a list of lists. For example, the composition of your final data structure should be similar to what is shown below.

[[‘Max’,’Medium’,’12345678’,58,152],[‘Jane’,’Johnson’,’87654321’,78,201],…[‘Sam’,’Spade’,’24681357’,16,30]]

Once you get your list constructed, print it so that I can verify that you’ve completed this part. Then go through your list and compute and output the gpa for each student in turn. Remember that gpa is quality points divided by credit hours.

For this lab, you may use any string or list functions or methods that you wish.

I know how to read in the whole file to a list:
myFile = open("Lab11.txt", "r")
myList = list(myfile)

but I am having trouble splitting each line from the file into another list

if i type
print(myList[0])
it will print the first line

please help with this assignment

3
Contributors
3
Replies
2 Days
Discussion Span
6 Months Ago
Last Updated
5
Views
aaron.jensen.923
Newbie Poster
1 post since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

just something to start with:

data_list = [line.split() for line in myFile.readlines()]
M.S.
Junior Poster
117 posts since Jul 2011
Reputation Points: 64
Solved Threads: 13
Skill Endorsements: 2

One step further:

data = ''' Max Medium 12345678 58 152
Jane Johnson 87654321 78 201
Bill Bupkiss 23456789 29 29
Nate Newby 98765432 0 0
Harold Humphries 11223344 43 160
Carol Cramer 22334455 102 400
Alvin Adams 33445566 67 120
Fred Frederick 44556677 81 250
Phillip Parker 55667788 44 168
Sam Spade 24681357 16 30 '''

import pprint

data_list = [[int(item) if len(item)<=3 and item.isdigit() else item for item in line.split()] for line in data.split('\n')]

pprint.pprint(data_list)
M.S.
Junior Poster
117 posts since Jul 2011
Reputation Points: 64
Solved Threads: 13
Skill Endorsements: 2

Using a oneliner seems to be very pythonic, but may be hard to comprehend for mere mortals. So here is an alternative with comments to help you:

# each line has these space separated data
# first last ID credit-hours quality-points 
raw_data = ''' Max Medium 12345678 58 152
Jane Johnson 87654321 78 201
Bill Bupkiss 23456789 29 29
Nate Newby 98765432 0 0
Harold Humphries 11223344 43 160
Carol Cramer 22334455 102 400
Alvin Adams 33445566 67 120
Fred Frederick 44556677 81 250
Phillip Parker 55667788 44 168
Sam Spade 24681357 16 30 '''

fname = "lab11.txt"
# write the test file out
with open(fname, "w") as fout:
    fout.write(raw_data)

# read the test file back in and process
with open(fname, "r") as fin:
    mylist = []
    for line in fin:
        temp_list = line.split()
        # convert credit-hours and quality-points to integers
        # these are at index 3 and 4 in the list
        temp_list[3] = int(temp_list[3])
        temp_list[4] = int(temp_list[4])
        mylist.append(temp_list)

# show result
import pprint
pprint.pprint(mylist)        

'''
[['Max', 'Medium', '12345678', 58, 152],
 ['Jane', 'Johnson', '87654321', 78, 201],
 ['Bill', 'Bupkiss', '23456789', 29, 29],
 ['Nate', 'Newby', '98765432', 0, 0],
 ['Harold', 'Humphries', '11223344', 43, 160],
 ['Carol', 'Cramer', '22334455', 102, 400],
 ['Alvin', 'Adams', '33445566', 67, 120],
 ['Fred', 'Frederick', '44556677', 81, 250],
 ['Phillip', 'Parker', '55667788', 44, 168],
 ['Sam', 'Spade', '24681357', 16, 30]]
'''
HiHe
Posting Whiz
332 posts since Oct 2008
Reputation Points: 177
Solved Threads: 34
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0688 seconds using 2.72MB