User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,782 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,557 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:

Finding top 10 values in csv file

Join Date: May 2008
Posts: 4
Reputation: ssDimensionss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ssDimensionss ssDimensionss is offline Offline
Newbie Poster

Re: Finding top 10 values in csv file

  #3  
May 9th, 2008
Originally Posted by jrcagle View Post
I'm a little confused about the use of the "num" variable.

I would probably do this:

  1. import urllib
  2. import csv
  3. temp_url = urllib.urlopen("http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_2007.csv")
  4. data = csv.reader(temp_url)
  5. header = data.next()
  6. earnings = [(row[0], float(row[6])) for row in data]
  7. earnings.sort(key = lambda x: x[1])
  8. earnings.reverse()
  9. earnings = earnings[:10]

line 6 generates a list of the companies and their earnings. Line 7 sorts these using the earnings as the key. Line 8 puts the highest earnings first. And line 9 trims the list to the top 10.

HTH,
Jeff





hey thx for the help, also can u tell me if i can do it like this?

import urllib
import csv
temp_url = urllib.urlopen("http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_2007.csv")
data = csv.reader(temp_url)
header = data.next()
max_company=''
max_earnings=0.0
num = 0
count = 0
while num < 10:
    for row in data:
        count += 1
        entry = row[6]
        if float(entry) > max_earnings:
            max_earnings = float(entry)
            max_company = row[0]
            
    num+=1
    print max_company

it still doesn work but if i cant find a way to exclude the line of data that is the max company, then when it reiterates it should print the second largest. and so on. thx again!
Reply With Quote  
All times are GMT -4. The time now is 4:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC