guys i couldn't solve this problem... I have a file 'text.CSV' like this:
milk,2.35
bread , 1.95
chips , 2.54
milk , 2.38
milk,2.31
bread, 1.90
def takes file and it should return this:
[('bread', '$3.85'), ('chips', '$2.54'), ('milk', '$7.04')]
How can i do that? i tried different ways!but i couldn't solve it My solution:
def calculate_expenses(filename):
file_pointer = open(filename, 'r')
data = file_pointer.readlines()
f=[]
f2=[]
for i in data:
x=i.split(',')
for j in x:
a=x[0].strip()
b=x[1].strip()
a=a.strip('\n')
b=b.strip('\n')
b=b.split()
b.insert(0,'$')
b="".join(b)
f.append(a)
f.append(b)
f2.append(f)
f=[]
break
return f2
filename='text.CSV'
print(calculate_expenses(filename))
it returns:
[['milk', '$2.35'], ['bread', '$1.95'], ['chips', '$2.54'], ['milk', '$2.38'], ['milk', '$2.31'], ['bread', '$1.90']]
can anybody help me?