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


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

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)

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

Start with:

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

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:
    print(row)
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:
    print(row)
    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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.