Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
import csv and then be able to add year revenue Posted by keith1569 on 11 Jul 2010 at 10:08 PM
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

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
Report
Re: import csv and then be able to add year revenue Posted by blemidon on 29 Aug 2010 at 8:34 AM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.