Country Gold Silver Bronze Total
US 9 15 13 37
GE 10 13 7 30
CA 14 7 5 26
NO 9 8 6 23
AU 4 6 6 16
RF 3 5 7 15
KO 6 6 2 14
CH 5 2 4 11
SW 5 2 4 11
FR 2 3 6 11


I have to take a file which will be formatted exactly like this (medal_count.txt) and take all the numerical values and plot a bar graph, using Quick Draw, which is completely useless, but I would greatly appreciate it if someone could help explain to me how to find the max and the min of a file like this (I can not use this EXACT example, as I will have use a different file with this format). I have been trying to put it into a string which I can later read using for loops but they are always seperate and it never seems to work. Any help would be great. All I am asking is how I could take a file like this, find the maximum and the minimum and using functions later on drawing the graph. Please note, I do also need to be able to find negative values as well!

import sys

file = sys.argv[1]

file = open(file, 'r')

lines = 0
bars = 0

for line in file:
line += 1

words = line.split(None)
bars += len(words)

print lines
print bars

I have done this to determine the x_variables and bar lines required. But I do not know a max or a min. Please, any help will be awesome. Thanks.

To point little mistake in thinking.

Your code with some comments.

import sys

file = sys.argv[1]

file = open(file, 'r')

lines = 0
bars = 0

## you are counting how many lines while reading
for line in file:
    line += 1

##line has the count of your lines
##something wrong maybe here??
words = line.split(None)
##do you want bar for words or maybe lines?
bars += len(words)

print lines
print bars

Maybe you could adapt this my code
http://www.daniweb.com/forums/thread274273.html

or http://www.daniweb.com/forums/thread273625.html

Basically you can use the built in functions max or min, but must take the part of list which has the numbers only.

Greetings,

Tony Veijalainen

P.S. That turtle graphics code (http://www.daniweb.com/forums/thread273625.html) has bug in separating the numbers, you must think how to fix the problem (hint: test with your data and see why the max is wrong)

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.