So basically what i want wanting to do is import a csv file and then have the user specify what year they want to add up all the total revenue.

here is what is in my csv file


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:

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.


Start with:

year= input("Enter the year you want to add up the total revenue: ")

Then loop over all lines which start with right year, change the amount part to float and add to sum.

You can use normal split() instead of csv in this simple case, if you did not invest in learning csv yet.

sorry for being so new, i was looking but cant find how can i loop over the lines and change the amount part to float?

You can use open statement as iterator in for. I do not want to give ready code. I did the code though. Try to read some code snippet code for inspiration. How have you studied and what kind of code have you written until now?

i have not written much code until now..i am reading Learning Python and Programming in Python 3.

I have been doing the exercises mainly from the books, but I wanted to give this project a try because it will be soemthing i will use at work

ok so i added a bit more but still running into issues

import csv

reader = csv.reader(open("/Users/michaelqualle/Desktop/revenue.csv", "rb"))
for row in reader:
year = raw_input("enter year you wish to check: ")
sum = 0
	col = row[0].split('|')
	if (year == col[0]
		sum += float(col[3])
print "Sum for " +year + " = + str(sum)

Are you retyping the code hand? Code has basic syntactic errors like 4 and 5 should be after lines 6 and 7 to make the indention right and you are also missing closing " in line 11? I gave you little bad variable name also as sum is built in function. This seemed to work:

import csv

reader = csv.reader(open("revenue.csv", "rb"))
year = raw_input("enter year you wish to check: ")
revenue_sum = 0
for row in reader:
    col = row[0].split('|')
    if (year == col[0]):
        revenue_sum += float(col[3])
print "Sum for " +year + " = "+ str(revenue_sum)

Here is some style sample of my own way of doing this without csv and writing some things differently to show alternative ways of writing things in Python:

year = raw_input("enter year you wish to check: ")
revenue_sum = 0.0
## using normal open and filtering the lines in iterator
for year,a,b,amount in (value.split('|') for value in open('revenue.csv') if value.startswith(year+'|')):
    revenue_sum += float(amount) ## last column by name from for
## year with 4 numbers, but it is in string format already, so string format, and money with $ and two decimals
print("Sum for %4s = $%.2f" % (year, revenue_sum))
""" Input:2009, Output:
Sum for 2009 = $984.93

This code is format enforcing version, if there is bad line in input with wrong number of items, it will complain with error message. However it does not care if other than requested year's lines have bad format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.