I have a function (main) that takes data from a csv file and converts it into a dictionary whose keys are the entries in the first column and their values are a list of all the other entries in that row (eg: one row is: 2020-12-20,0,0,0,0,206,
so the key is 2020-12-20
and the rest of the entries are strings in a list: ['0', '0', '0', '0', '206']
):
def main():
import csv
# doses_data_mar_20.csv
dict_doses_by_date = {}
filename_input = str(input("Please enter a .csv file to read: "))
with open(filename_input, "r") as inp, open('doses.csv', 'w') as out:
header = inp.readline()
reader = csv.reader(inp, delimiter=",", quotechar='"')
for line in reader:
dict_doses_by_date[line[0]] = line[1:6]
return dict_doses_by_date
def count_doses_by_date(dict_dose_by_date):
now I need to define a new function count_doses_by_date
that takes each list of strings as an input and converts each of these lists of strings into a list of integers and add all the integers to get their totals. then outputs this into another csv file.
I tried doing this:
def count_doses_by_date(dict_dose_by_date):
import csv
# doses_data_mar_20.csv
dict_doses_by_date = {}
filename_input = str(input("Please enter a .csv file to read: "))
with open(filename_input, "r") as inp, open('doses.csv', 'w') as out:
header = inp.readline()
reader = csv.reader(inp, delimiter=",", quotechar='"')
for line in reader:
dict_doses_by_date[line[0]] = line[1:6]
for k in dict_doses_by_date:
list_integers = [int(x) for x in dict_doses_by_date[k]]
sum_integers = sum(list_integers)
print_value = "{}, {} \n".format(k, sum_integers)
return out.write(print_value)
but I’m getting errors since some of the lists contain strings like '1,800' which contain commas that prevent it from be converted to an integer. I don't know how to get rid of there's thousands commas without disrupting the commas that separate the csv values.
I'm stuck.. how would this be done?