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) and here is the text file link
http://pages.cpsc.ucalgary.ca/~zongpeng ... rades1.txt
any help would be appreciated

Dani AI

Generated

A concise, practical plan to get from the attached text file to the counts you need for a pie chart: identify which column holds the numeric score, skip the header and any trailer/EOF marker, convert scores to numbers (strip percent signs if present), map each numeric score to a grade bucket, and count occurrences (then convert counts to percentages or angles for QuickDraw). This builds on suggestions from , and the observation by that the file uses tab-separated rows and contains a header plus an EOF marker. (daniweb.com)

Use a tolerant parser (splitting on whitespace handles tabs) and guard conversions with try/except so malformed lines get skipped instead of crashing the program. The example below shows a compact, reusable approach: a function to map a numeric score to a label, a parser that finds the score column by index, and a Counter to tally buckets.

from collections import Counter

def grade_label(score, cutoffs):
    for thresh, label in cutoffs:
        if score >= thresh:
            return label
    return cutoffs[-1][1]

def parse_counts(path, grade_index, cutoffs):
    counts = Counter()
    with open(path, 'r') as f:
        next(f, None)             # skip header
        for line in f:
            s = line.strip()
            if not s or s.lower().startswith('end') or 'eof' in s.lower():
                continue
            parts = s.split()     # handles tabs and spaces
            try:
                raw = parts[grade_index].rstrip('%')
                score = float(raw)
            except (IndexError, ValueError):
                continue
            counts[grade_label(score, cutoffs)] += 1
    return counts

Choose cutoffs as a descending list, e.g. [(90,'A'),(80,'B'),(70,'C'),(60,'D'),(0,'F')]. To feed QuickDraw, compute angles with angle = count/total * 360 (one angle per label) or percentages with count/total*100. Quick checks: print header.split() and the first data line to confirm the grade_index, handle percent signs or trailing text, and watch for BOM/encoding or CRLF issues when opening old files. These steps produce robust counts you can pass straight to your pie-drawing routine.

Recommended Answers

All 7 Replies

Assuming you already know how to read in from files, there's a wonderful plotting tool for python called matplotlib that will let you make your pie chart. You'll have to get numpy first, though.

Btw, your link to the text file is broken.

try using link at the top toolbar of your editor. Very helpful to post links.
As buddy said, Matplotlib+wxPython will give you total control. If you do simple plotting, then wxPython have tool for that.

Anyway what do yo want to do with grades? May be python-mysql+wxPython+Matplotlib is all you need

unfortunatly i'm only allowed to use quickdraw,
i'm only struggling in figuring out how to extract the data from the txt file and assign the numbers to grades ,count how many scored each grade, i know how to make the chart (A1)
http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A3/grades1.txt
any help or tips would be appreciated

Ok, do us a favour,
attach that file to your post. Jus Click reply and you will see button to attach and do it. We cant know the content of the file!

Do it please :)

I missed the Link to the file.
As far as I can see, you need to look at csv module to help you to do it.
What I propose is:
1. Open file
2. Get contents of one line
3. get that line with delimiter set to space ie ' '
4. Do whatever you want with data
NB: you can print the result to see what it produces before you use the data. Remember to use:

csv.reader(open('grades1.txt'), delimiter=' ')

More links:
http://docs.python.org/library/csv.html

http://tinyurl.com/yf3pqka
Hope it helps

Actually. your raw data file is tab delimited and easily converted into a list of lists. Once that is done you can simply access your specific data by index and looping through the list.

The only special considerations are that the first line is a header and the last line is an 'end of file' marker.

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) . any help would be appreciated

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.