I have a CSV File here: http://www.filedropper.com/excel which looks like this http://pastebin.com/ammzv4FK.
What I want to do is this:
Take each year with its corresponding rows and find the numbers in months and total them and assign them to variables.
So the first year, 2000, the list would look like this:
year2000 = ['14744', '2947', '14905', '1748', '2859', '11778', '1453', '5255', '14806', '1858', '10763', '6000']
Then once I have that list I can convert the elements in the list from strings to integers, and sum them using the sum() command.
I can print the rows out but I’m having trouble excluding the year number and then storing the rest into variables because my list comes out as this:
['2000, 14744, 2947, 14905, 1748, 2859, 11778, 1453, 5255, 14806, 1858, 10763, 6000']
as one big string, instead of individual elements, and my list prints all the years out at once so I can’t seem to figure out how to store them into variables and total them separately.
Here is my code:
with open("file.csv","r") as f:
for i in range(1):
next(f)
for x in f:
x=x.split()
print (x)
But when print(x) runs it prints it all, how can I store each year with its contents in a variable while excluding the actual year number?
I’m using Python 3, thanks.
If you are getting the row like that you probably aren’t using a csv reader. You should use the
csvmodule and store each row in adict, with the year as the key. This only works if your data consists of integers.To get the sum, use the built in
sumfunction.