4 Replies - 844 Views - Last Post: 26 January 2013 - 05:52 PM Rate Topic: -----

#1 CaseyJames22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 26-January 13

Trying to create a Student and Grades file after dropping a .csv

Posted 26 January 2013 - 01:39 PM

Description:
Aside from the file I/O part, this should all be a review.
A .csv file is one that is "dumped" from a spreadsheet program. It is a text file that contains the rows of the spreadsheet as lines and the fields of each line are comma separated (hence the .csv ... character separated values). If you need more info, find an informative web site.

I will supply you with a .csv file with the following properties

there is no line containing field headings ... only data
the lines are composed of student / grade data (in order)
student first name
student surname
12 assignment scores
12 quiz scores
4 exam scores
the character separating the data items on each line is a comma
missing scores and/or empty fields are indicated by a '-' (minus sign ... no quotes)
the first line of the file has
first name 'possible'
surname is empty
max possible score for each corresponding item (think spreadsheet)

You will write a Python script that will:

ask the user for an input (.csv) file name (don't have your program die an ugly death if the user enters a name of a file that cannot be opened for reading)
the suggested name for the output file you will create should be the same as your input file ... up to the first dot or end of the name if it doesn't contain one ... and have a '.grd' extension. The user may provide an alternative name.
read and process the data according to the rules below
all assignment scores are kept to form the assignment average
the ten highest quiz scores are kept to form the quiz average(quiz scores are all max 10 points)
the lowest exam score can be dropped and the rest are averaged to form the exam average
the final weighted average score is calculated as a 45% exam average + 10% quiz average + 45% homework
have output in columns with headings centered above the data
Name
HWavg
QZavg
EXavg
Final
print names as: LastName, FirstName
order the 'rows' of data alphabetically, according to the Names field
print averages in the form XXX.XX ... that is, with two places of accuracy ... the decimal points must align down the entire columns
write the output to the file suggested above.

Finally, your script must be clearly documented.
Data and Example Output Files:
You can find the data file for this assignment in the data folder of my c153 repo at Bitbucket under the name grades.csv. In the same folder, there is a file named grades.grd, which is an example of the output format you should be generating. (Note: it is an example ... your actual data MAY NOT agree ... YOU check your work. If you generate bogus data, your script is WRONG)

Some Observations / Ideas / Hints:

Use the online python documentation when you don't remember / don't know details or want to find methods for objects
Modularity might be nice .... You can
convert the line to a list,
peel off the list items concerned with a particular calculation,
pass them to a function that returns the average in question.

Submission:

Name your script file grades.py and your output data file grades.grd,
Place your files in a folder named you.grades
archive your folder as a .tar.gz file named you.grades.tar.gz ... (see the video on creating an archive file on our moodle site)
Submit the file using the controls on the assignment page of our class moodle Web site.


what i have....
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# file grades_example.py
# author: 
# Example of I/O for the "grades" assignment 1 and of "stub" programming

'''
This script reads an input .cvs file with:
    first name
    surname
    12 homework grades
    12 quiz scores (10 pts each)
     4 exam scores (100 pts each)
line at a time and reports
    first and surname
    averages for each category of score
    averall average and letter grade, per the schedule:
        homework - 40%; quizes - 10%; exams - 50%
        [100, 90] - A; (90, 80] - B; (80, 70] - C; (70, 60] - D; (60, 0] - F
'''

from FileUtilities import openFileReadRobust as openRead, \
                          openFileWriteRobust as openWrite

# Stub function for processing student data                          
def processStudent(data, possible):
    line=sum([0 if k == '-' else int(k) for k in line[2:14]])/totals[hwtot]
    return ["blow, joe", 53, 26, 12, 34]

# Stub function for setting up files for I/O
def setUpIO():
    return infile, outfile 
    # return inFile, outFile

# Opens the input and output files
infile = open('grades.csv','r')
outfile = open('grades.grd','w')

# Get the homework, quiz, and exam possible totals
line = infile.readline().rstrip('\n').split(',')
hwtot = sum([0 if k == '-' else int(k) for k in line[2:14]])
qztot = 100
extot = 300
totals = [hwtot, qztot, extot]

gradelist = []

# For every line in the file, compute the averages and final grade
for line in infile:
    # process line and add it to the list of grades
    stuData = processStudent(line, totals)
    
    # Add student to grade list
    # Note: this next step could have been done much more elegantly, but
    #       I wanted you to see the formatting
    gradelist.append('{:<16}  {:8.2f}  {:8.2f}  {:8.2f}  {:8.2f}'.format\
                        (stuData[0], stuData[1], stuData[2], stuData[3],
                         stuData[4]))

# Sort list and provide a heading
gradelist.sort()
gradestr = '{:^16}' + 4 * '  {:>8}'
gradelist.insert(0, gradestr.format('Name', 'HWavg', 'QZavg', 'EXavg', 'Final'))

# Write the list to the output file
outfile.write('\n'.join(gradelist) + '\n')

# Close files
infile.close()
outfile.close()




Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create a Student and Grades file after dropping a .csv

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3252
  • View blog
  • Posts: 10,903
  • Joined: 12-December 12

Re: Trying to create a Student and Grades file after dropping a .csv

Posted 26 January 2013 - 01:42 PM

So what is the specific question that you are asking?
Was This Post Helpful? 0
  • +
  • -

#3 CaseyJames22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 26-January 13

Re: Trying to create a Student and Grades file after dropping a .csv

Posted 26 January 2013 - 01:44 PM

once the file is imported how do i run thru it line by line and pull out specific slices to divide by the totals...?

i feel like that can be done in the processStudet function
Was This Post Helpful? 0
  • +
  • -

#4 CaseyJames22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 26-January 13

Re: Trying to create a Student and Grades file after dropping a .csv

Posted 26 January 2013 - 03:38 PM

This is my table..
possible	-	25	25	25	15	25	10	50	50	20	40	60	50	10	10	10	10	10	10	10	10	10	10	10	10	100	100	100	100
Barney	Miller	25	25	25	13	22	-	50	21	5	18	-	-	6	6	2	10	0	5	8	-	-	-	-	-	68	55	45	56
Robert	Johnson	25	25	25	-	25	-	50	26	-	31	57	16	6	4	6	10	0	3	8	8	6	10	6	10	66	69	91	75
Bonnie	Raitt	25	25	25	12	25	10	50	46	20	35	43	35	9	6	-	10	2	7	6	-	-	4	-	10	87	85	74	82
Albert	King	25	25	25	13	25	10	50	50	20	37	56	48	9	10	-	10	4	10	8	-	10	10	6.67	10	89	94	98	93
Sam	Spade	23	13	25	10	25	6	50	22	-	19	22	25	-	2	4	10	-	5	-	-	8	-	6.67	4	61	42	46	49
Elwood	Blues	23	16	25	9	20	9	50	22	5	34	22	27	3	2	2	10	0	2	8	6	6	-	-	4	49	49	44	47
Loren	Michaels	23	23	25	13	25	10	50	0	12	37	52	43	5	6	8	10	-	2	8	10	4	8	6	6	57	57	58	57
John	Jones	23	25	25	12	25	10	50	50	20	39	56	43	8	6	10	10	4	5	8	10	10	8	8	10	79	69	95	81
Maria	Stumph	23	20	25	12	22	9	50	42	-	40	33	43	8	6	-	10	0	5	6	-	2	4	8	10	59	76	52	62
Lawrence	Lanning	21	-	25	0	13	9	50	35	-	5	18	23	-	4	4	10	0	5	6	2	4	10	-	10	53	60	54	55
Fred	Flintstone	21	22	25	13	-	-	50	17	14	38	57	46	6	8	6	10	4	8	10	10	8	10	10	8	76	79	87	80
Betty	Rubble	21	25	25	13	25	-	50	50	13	40	58	49	7	8	4	10	3	6	8	10	8	10	8	10	89	90	94	91
Steve	Miller	19	23	25	9	6	10	-	39	5	7	-	0	5	4	8	10	-	2	4	-	-	-	-	-	51	67	-	59
Graham	Parker	19	25	25	13	5	10	50	38	5	37	57	0	5	6	6	10	0	-	8	6	8	6	4.44	4	64	81	69	71
Nash	Rambler	19	25	25	13	25	10	50	50	15	40	51	25	8	6	6	10	2	5	8	8	0	4	4.44	8	66	63	73	67
Ed	Belle	17	25	25	8	25	6	50	44	-	32	41	25	5	8	8	10	4	4	8	6	6	8	4.44	-	53	43	42	46
Marlin	Perkins	17	13	25	10	5	4	50	24	5	38	42	31	6	4	2	10	2	2	8	6	6	10	8	10	22	30	32	28
Randy	Oldman	17	21	25	11	25	-	50	0	-	39	28	0	7	6	6	10	2	5	6	6	8	4	10	8	62	59	38	53
Bill	Munroe	16	23	-	12	5	10	-	41	-	37	-	9	6	6	2	10	0	2	8	-	6	-	4.44	10	48	33	33	38
Ester	Bumknee	14	20	25	9	24	-	50	0	-	-	-	-	-	6	-	10	0	-	-	-	-	-	-	-	68	63	-	65
Jackson	Greene	13	25	25	13	25	10	50	50	5	29	-	0	7	8	6	10	0	2	10	-	-	-	-	-	71	57	-	64
Al	Hurt	0	24	-	12	15	9	-	0	-	-	-	0	5	4	4	10	-	2	-	-	-	-	-	-	-	27	-	27
Hanna	Montana	0	25	-	13	-	-	-	0	-	-	-	-	6	2	8	10	0	-	-	-	-	-	-	-	41	28	-	34



so each line needs to be broken into list. and then the quizes, homework, and exams...
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Trying to create a Student and Grades file after dropping a .csv

Posted 26 January 2013 - 05:52 PM

read each line into a string and make each string an element in a list
with open(filename) as f:
    lines = f.readlines()



Split a line into a list
splitLine = line.split()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1