Sum and Multiplication with items of a CSV

Asked

Viewed 187 times

1

good night! I’m new to programming and I’m learning to code in python. Also, I’ve been trying to create something that’s relatively simple, but I’m finding it difficult to execute.

I imported a list of items from csv, which are basically: Item and Percapita; I did all the importing and transformation to lists:

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

Now my difficulty is to have the items multiplied by the number of guests and disappear. Below is the list of items.

['AUSTRALIAN BREAD, '0.0007 n'] ['CROISSANT BREAD', '0.106 n'] ['LARGE OLIVE OIL BREAD', '0.0027 n'] ['PAO DE FORMA', '0.0089 n'] ['PAO DE FORMA INTEGRAL', '0.0057 n'] ['CHEESE BREAD', '0.014 n'] ['SWEET BREAD WITH FRUIT', '0.0036 n'] ['COCONUT BUN', '0.0022 n'] ['CREAM BUN', '0.0036 n'] ['PÃO DOCE GOIABADA', '0.0024 n'] ['FRENCH BREAD 50G', '0.0084 n'] ['FRENCH BREAD MINI 25g', '0.0084 n'] ['WHOLE BREAD C/ OATS', '0.0015 n'] ['ITALIAN BREAD C/ CALABRESA', '0.0016 n'] ['BREAD HONEYMOON', '0.0036 n'] ['RECIFINE BREAD', '0.0032 n'] ['SEDINHA BREAD', '0.004 n'] ['SEVEN GRAIN BREAD', '0.0031 n'] ['Chocolate milk', '0.0257 n'] ['PLUM YOGURT', '0.0081 n'] ['STRAWBERRY YOGURT', '0.0137 n'] ['SKIMMED MILK', '0.007 n'] ['WHOLE MILK', '0.0588 n'] ['PINEAPPLE JUICE', '0.0142 n'] ['ACEROLA JUICE', '0.0343 n'] ['CASHEW JUICE', '0.176 n'] ['SUCO DE GOIABA', '0.162 n'] ['MANGO JUICE', '0.243 n'] ['TANGERINE JUICE', '0.213 n'] ['JUICE DETOX', '0.0219 n'] ['CASHEW JUICE', '0.0219 n'] ['PINEAPPLE MEDIO', '0.0501374969984703 n'] ['GOIABA', '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'] ['SMOKED TURKEY BREAST SLICED', '0.008 n'] ['SLICED COOKED HAM', '0.00803951120728223 n'] ['SLICED CURD', '0 n'] ['QUEIJO MINAS', '0.00956485890977167 n'] ['SLICED MOZZARELLA', '0.0050199203187251 n'] ['CHEESE SLICED DISH', '0.00999494167334197 n'] ['SALAME ITALIANO', '0.008 n'] ['COOKED BANANA', '0.0134 n'] ['SWEET POTATO', '0.0219 n'] ['CHARQUE MEAT', '0.0173 n'] ['BEEF SUN BAIT', '0.0193 n'] ['CORN COUSCOUS', '0.0184 n'] ['YAM', '0.0145 n'] ['LINGUIÇA FINA', '0.0201 n'] ['Macaxeira', '0.11 n'] ['MINI MIXED', '0 n'] ['SCRAMBLED EGGS', '0.0166 n'] ['PAPA DE AVEIA', '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'] ['ANT CAKE', '0.0029 n'] ['BOLO INGLES', '0.004 n'] ['DIET ORANGE CAKE', '0.0026 n'] ['MIXED CAKE', '0.0039 n'] ['BOLO SOUZA LEÃO', '0.0105 n']<

Anyway, does anyone have any idea how to help me? Thank you!

  • What you can’t do, it wasn’t very clear in your question

  • In a csv of 2 columns and x rows, what you want is to basically multiply column 1 by column 2 of the first row, and add with the multiplication of column 1 by column 2 of the second row and so on ?

  • Sorry for the lack of clarity. What I want to do is basically: Menu Item = Percapita * Guest Number With the 'Menu Item' column A of the file, 'Percapita' column B of the file and Guest Number a variable in which I will enter randomly. .

  • I understand what you want to do, I just don’t understand why you can’t. What your difficulty, can’t access the list element to mutiplicate, can’t convert to float, Can’t add to the final result? This can help you solve your doubts

  • It was to access the same element, but the code below worked! Thanks for the help!!

1 answer

0


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

In that 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.