Adding and multiplying with items from a CSV

Good night! I am new to programming and am learning to program in python. Also, I have been trying to create something that is relatively simple, but I am finding it difficult to execute.

I imported a list of items from csv, which basically are: Item and Perc; I did all the import and transform to lists:

import csv
arquivo = open('mhpercapitacm.csv')
linha = csv.reader(arquivo)
for linha in arquivo:
    linha = linha.split(";")

Now my difficulty is getting items to multiply by the amount of guests and add up. Follow below the list of items.

['Australian bread', '0.0007\n'] ['CROISSANT bread', '0.106\n'] ['Large olive oil bread', '0.0027\n'] ['Shape PAO', '0.0089\n'] ['INTEGRAL form PAO', '0.0057\n'] ['Cheese PAO', '0.014\n'] ['Sweet bread with fruit', '0.0036\n'] ['Sweet coconut bread', '0.0022\n'] ['Sweet cream bread', '0.0036\n'] ['Sweet guava bread', '0.0024\n'] ['French bread 50g', '0.0084\n'] ['French bread MINI 25G', '0.0084\n'] ['WHOLEMEAL BREAD WITH OATS', '0.0015 \ n'] ['Italian bread C / CALABRESA', '0.0016\n'] ['Honeymoon bread', '0.0036\n'] ['PÃO RECIFINHO', '0.0032\n'] ['SEDINHA bread', '0.004\n'] ['Seven grain bread', '0.0031\n'] ['Chocolate', '0.0257\n'] ['Plum yogurt', '0.0081 \ n'] ['Strawberry yogurt', '0.0137\n'] ['Skim milk', '0.007\n'] ['Whole milk', '0.0588\n'] ['Pineapple juice', '0.0142\n'] ['ACEROLA juice', '0.0343\n'] ['Cashew juice', '0.176\n'] ['Guava juice', '0.162\n'] ['JUICE MANGA ', '0.243\N'] ['Tangerine juice', '0.213\n'] ['Detox juice', '0.0219\n'] ['SUCO box', '0.0219\n'] ['Medium pineapple','0.0501374969984703\n'] ['Guava','0.00739496845444968\n'] ['MAMAO FORMOSA','0.0683925779441316\n'] ['MARACUJA','0.00643367648577763\n'] ['Watermelon','0.0646550130974509\n'] ['Spanish melon','0.0466495073368142\n'] ['Fruit salad', '0.0229433868317184\n'] ['Tangerine', '0.0064\n'] ['Sliced smoked turkey breast', '0.008\n'] ['HAM Sliced baked','0.00803951120728223] ['Sliced Curd Cheese', '0 \ n'] ['Cheese mines','0.00956485890977167\n'] ['Sliced mozzarella cheese', '0.0050199203187251\n'] ['Sliced dish cheese', '0.00999494167334197\n'] ['Italian salami', '0.008\n'] ['Boiled BANANA', '0.0134\n'] ['Sweet potato', '0.0219\n'] ['CHARQUE meat', '0.0173\n'] ['Sun bait meat', '0.0193\n'] ['Corn couscous', '0.0184 \ n'] ['Yams', '0.0145\n'] ['Thin sausage', '0.0201\n'] ['MACAXEIRA', '0.11\n'] ['Mini mixed', '0 \ n'] ['Scrambled eggs', '0.0166\n'] ['Oatmeal', '0.0166\n'] ['Carrot cake', '0.0029 \ n'] ['Chocolate cake', '0.0048\n'] ['Orange cake', '0.0035\n'] ['Roll cake', '0.0103\n'] ['Tapioca cake S / GLUTEM', '0.0034 \ n'] ['Tingling cake', '0.0029\n'] ['BOLO INGLES', '0.004\n'] ['Diet orange cake', '0.0026\n'] ['Merged cake', '0.0039\n'] ['BOLO Souza Leon', '0.0105\n']

Anyway, someone has any idea how to help me? Thank you!

Author: Rachid Elihimas, 2019-08-12

1 answers

Well, the menu item is accessed by Line[1] as string. Then you need to convert to float if you want to multiply by the number of guests: float (row[1]).

In this case:

import csv
arquivo = open('mhpercapitacm.csv')
linha = csv.reader(arquivo)
numeroHospedes = 10;
valorTotal = 0;

for linha in arquivo:
    linha = linha.split(";")
    item = float(linha[1])
    valorCalculado = item * numeroHospedes
    print (valorCalculado)
    valorTotal += valorCalculado

print(valorTotal)
 0
Author: brenodiogo, 2019-08-13 01:41:12