Hi i have a CSV file that looks something like this:
Name ASX_Code Date SharePrice
ACACIA RESOURCES AAA 19990630 1.75
ACACIA RESOURCES AAA 19980630 1.72
ABSOLUTE RETURN FUND UNIT AAB 20040625 0.9
ABSOLUTE RETURN FUND UNIT AAB 20030630 0.85
AUSTRALIAN AGRICULT. AAC 20070629 2.95
AUSTRALIAN AGRICULT. AAC 20060630 1.935
AUSTRALIAN AGRICULT. AAC 20050630 1.71
AUSTRALIAN AGRICULT. AAC 20040625 1.23
AUSTRALIAN AGRICULT. AAC 20030630 1.08
AUSTRALIAN AGRICULT. AAC 20020628 0.67
AGRI ENERGY LIMITED AAE 20070629 0.325
AUSTRALIAN ETHANOL AAE 20060630 0.585
AUSTRALIAN ETHANOL AAE 20050630 0.26
ALCOA INC. CDI AAI 20070629 47
ALCOA INC. CDI AAI 20060630 42.75
ALCOA INC. CDI AAI 20050630 39.99
ALCOA INC. CDI AAI 20040625 46.25
ALCOA INC. CDI AAI 20030630 66
ALCOA INC. CDI AAI 20020628 66.6
A1 MINERALS LIMITED AAM 20070629 0.195
A1 MINERALS LIMITED AAM 20060630 0.185
A1 MINERALS LIMITED AAM 20050630 0.26
A1 MINERALS LIMITED AAM 20040625 0.52

well yeah it looks something like that its the data of a list of companies and their shapes prices in different dates. I was asked to write a code on which company has the higghest share value and dont have much of a clue as to how to start can any1 help? thx

Member Avatar for kdoiron

How do you think it should be approached? What do you think needs to be done? Let's hear your ideas first - even in English rather than in Python, if that's easier. We can take it from there.

Python has a module csv that will make your life easier handling csv files.

Assuming your data actually has commas in the appropriate places, the following code will determine the company with the highest stock price:

import csv

f = open(your_data.csv)
reader = csv.reader(f)
# save header items
headerList = reader.next()
# Create a dictionary with company name and stock prices
dataDict = {}

for line in reader:
    dataDict.setdefault(line[0], []).append(line[-1])  
f.close()

dataList = [[max(value), key] for key, value in dataDict.iteritems()]
dataList.sort()
print '%s had the highest stock price of $%0.2f' % (dataList[-1][1], float(dataList[-1][0]))

Output:
>>> ALCOA INC. CDI had the highest stock price of $66.60

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.