Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

import csv and then be able to add year revenue

keith1569keith1569 Posts: 1Member
here is my csv file
[quote]
2009|1|22|56.23
2009|4|12|86.24
2009|5|20|34.67
2009|6|22|12.67
2009|3|21|795.12
2008|7|4|753.43
2008|8|9|2345.45
2008|9|1|2357.23
2010|5|2|7552.21
2010|3|15|666
2010|4|17|2323.75
2010|5|19|2367.23
2010|6|25|6453
2010|2|6|1267.12
[/quote]

here is my code

[code]
import csv

print ("Enter the year you want to add up the total revenue.")


reader = csv.reader(open("/revenue.csv", "rb"))
for row in reader:
print(row)
[/code]

here is what i have so far...sorry i am very new to python but am really enjoying it. any help would be greatly appreciated.

thanks
keith

Comments

  • blemidonblemidon Posts: 22Member
    You have to split the row:

    values = row.split('|')

    this create a tuple of the values:

    ('2009', '1', '22', '56.23')

    You can then access the columns by
    values[0] etc.
    Be aware that this values are strs.
    You need to convert them to float if you want to make a sum.

    v = float(values[3].strip())

    Here v will be 56.23
    strip() removes whitespaces, just to be sure.

    Hope it helps:
    Imre Horvath
Sign In or Register to comment.