| | |
neeed help!!!
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
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/~zongp...ents/A3/a3.pdf
any help would be appreciated
txt file grades1.txt
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/~zongp...ents/A3/a3.pdf
any help would be appreciated
txt file grades1.txt
0
#2 25 Days Ago
•
•
•
•
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/~zongp...ents/A3/a3.pdf
any help would be appreciated
txt file Attachment 12416
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
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#4 25 Days Ago
here is what i have so far but it doesn't seem to be working need help in figuring out what's wrong
Python Syntax (Toggle Plain Text)
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))
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
0
#5 25 Days Ago
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.
Python Syntax (Toggle Plain Text)
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))
Last edited by woooee; 25 Days Ago at 12:14 am.
Linux counter #99383
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
0
#6 25 Days Ago
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:
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.
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.
python Syntax (Toggle Plain Text)
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.
python Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
0
#8 25 Days Ago
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.
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])
Now if you want a list that only contains the stuff in the 6th column, you can use this code.
python Syntax (Toggle Plain Text)
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])
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#9 25 Days Ago
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
if you have any suggestion on how to solve this that would be great
thx again for your help i really apperciated
i was using that code, but it didn't work
Python Syntax (Toggle Plain Text)
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
thx again for your help i really apperciated
![]() |
Other Threads in the Python Forum
- Previous Thread: Drawing stick figure?
- Next Thread: i would pay money for the help
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session signal simple skinning sprite string strings syntax terminal text threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape whileloop wxpython






