0

I'm currently working on this assignment where I have to convert essentially this input csv file

into this written csv file

here's my code that's currently creating the csv file in my folder but it's not actually writing anything in it.

import csv

def read_calculate():
    dict1 = {}
    with open('sunspots.csv','r') as file:
        csv_reader = csv.reader(file, delimiter=',')
        for row in csv_reader:
            year_month = row[0] + row[1]

            if year_month in dict1:
                if(row[4].isnumeric() and int(row[4])!= -1):
                   dict1[year_month]+= int(row[4])
                else:
                   if(row[4].isnumeric() and int(row[4])!=-1):
                       dict1[year_month]=int(row[4])
                   else:
                       dict1[year_month]=0

        file.close()
        return dict1


def write_to_file(dict1):
    with open('Monthtotal.csv','w',newline='') as write_file:
        writer = csv.writer(write_file)
        for key in dict1.keys():
            line = key[0:4],k[4:6],str(dict1[key])
            writer.writerow(line)

    write_file.close()



if __name__=='__main__':
    dict1 = read_calculate()
    write_to_file(dict1)
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
sriracha
  • 13
  • 4
  • We cannot test your code with those images. – Jongware Nov 03 '18 at 21:29
  • Possible duplicate of [portable way to write csv file in python 2 or python 3](https://stackoverflow.com/questions/38808284/portable-way-to-write-csv-file-in-python-2-or-python-3) –  Nov 03 '18 at 21:35
  • @DManokhin thanks for mentionning this excellent Q&A :) but this isn't the issue. OP is obviously using python 3 and uses the proper `newline=""` statement. – Jean-François Fabre Nov 03 '18 at 21:36
  • BTW you don't need the `close()` statements since you're using contexts for your files. – Jean-François Fabre Nov 03 '18 at 21:37

2 Answers2

2

the test

if year_month in dict1:

"protects" dict1 from being updated when reading the file. So when you write the file you get no data.

I suppose you want to deindent the else part:

if year_month in dict1:
    if(row[4].isnumeric() and int(row[4])!= -1):
       dict1[year_month]+= int(row[4])
else:
   if(row[4].isnumeric() and int(row[4])!=-1):
       dict1[year_month]=int(row[4])
   else:
       dict1[year_month]=0

but there are smarter approaches like collections.Counter:

import collections
dict1 = collections.Counter()

then in the loop, just a test to ensure that the data is a number then:

if(row[4].isnumeric() and int(row[4])!= -1):
   dict1[year_month] += int(row[4])
else:
   dict1[year_month] += 0  # add nothing, but "creates" the key if doesn't exist

you need the else part (which seem clumsy, yeah I know) so you create all month keys, even empty ones, else your csv file will have holes. the alternative is not to loop on the keys when writing your dictionary (dicts aren't ordered until python 3.6) but on the hardcoded month values. Counter will yield 0 on a non-existing key.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

dict1 starts empty, and you only add to it if the year_month value is already found in that empty dict, which it obviously never will be.

I'm not quite sure what the logic is supposed to be here, but you presumably need some way of setting a default value when the year_month is not found.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895