hi, i'm new in python programming
i'm trying to write a simple program that processes a text file containing grades for a class, extracts the desired grades, count the number of grades in each grade segment and genrate a pie chart for grades,(A1) .
http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A3/a3.pdf
any help would be appreciated
txt file
grades1.txt

Recommended Answers

All 8 Replies

Member Avatar for masterofpuppets

hi, i'm new in python programming
i'm trying to write a simple program that processes a text file containing grades for a class, extracts the desired grades, count the number of grades in each grade segment and genrate a pie chart for grades,(A1) .
http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A3/a3.pdf
any help would be appreciated
txt file [ATTACH]12416[/ATTACH]

http://www.daniweb.com/forums/announcement114-2.html
try to make a plan for what the program needs to do and divide each step into separate functions. :)

Hint: f = open( filename, "r" ) opens a file named 'filename'
l = f.readlines() reads the contents of the file line by line into a list
string.split() split a string by a given delimiter as an argument

Simply posting the same question with another meaningless title again is in rather poor taste. People have given you advice, so apply it and go from there. We will not do homework for you, since you are the one that has to learn something!

here is what i have so far but it doesn't seem to be working need help in figuring out what's wrong

Code:
dbfile = open(grades.txt,'r')
  table = []
  lines = dbfile.readline()
  newtable = []
  for line in lines:  
   line = line[:-1]
   r = string.split(line,':')
   table.append(r)
  for x in table:       t=[]
   for all in x:
    if all.isalpha() == 0:
     num = float(all)
    else:
     num = all
   t.append(num)
   newtable.append(tuple(t))

You want to use "readlines()",as readline reads one line at a time, where readlines() reads all data. Also, a print statement is added for clarity.

records = open(grades.txt,'r').readlines()
table = []
newtable = []
for line in records:  
   line = line[:-1]
   r = string.split(line,':')
##   table.append(r)
##  for x in table:           ## x = r so just use r
##   for all in x:
   for all in r:
    print "testing isalpha()", all.isalpha(), all
    if all.isalpha() == 0:
     num = float(all)
    else:
     num = all

##---  get the first part of the program working first
##   t.append(num)
##   newtable.append(tuple(t))

Remember, the first thing you want to do is read in the input, so start by working out that problem. Read in the file and strip off the first and last lines:

infile = open('grades1.txt','r')
all_lines = infile.readlines()

all_lines = all_lines[1:]
all_lines = all_lines[:-2]

Then you could take your file and make it into a list of tuples so you can generate reports based on any field in the file.

dataset = []
for eachline in all_lines:
   dataset.append(eachline.split('\t'))

So that is just one way that you can read in the data. If you decide to go this route, then you can iterate through the list called dataset and pull out the field of the tuples that you're interested in.

i'm really confused , i tried many ways to read and extract the 6th column from the txt file (A1) but it doesn't work ,
if someone can help me i would really appreciated plz without mocking me i'm a real newbie in this language

Use the first code snippet that I put in my last post so that you can strip off the first and last lines of the file.

Now if you want a list that only contains the stuff in the 6th column, you can use this code.

6th_column = []
for eachline in all_lines:
    6th_column.append(eachline.split('\t')[5])

So what's going on in my code snippet? Well first I'm creating an empty list that I can fill with stuff called 6th_column. Then in the for loop I start adding stuff to my list with the append. I'm splitting each line into a tuple with the split function, and I'm specifying that tabs are what separate each column.

How did I know that it was tabs rather than spaces? Well I read in a line of the file earlier and used the repr() function to see what they were.
print repr(all_lines[0])

thx man for your help, i'm now trying to assign grades for each result in column #6 and count how many repeated grades scored
i was using that code, but it didn't work

A+ =[10]
A = [9,10)
A- =[8,9)
B+ =[7,8)
B =[6,7)
B- =[5,6)
C+ =[4,5)
C =[3,4)
D+ =[2,3)
F =[0,1)


grades=["ABCDA"]
i=0
total=0
while i<len(grades):
    if grades[i]  =='A':
        total=total+1
        i=i+1
print total

if you have any suggestion on how to solve this that would be great
thx again for your help i really apperciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.