0
Hello, I’m making a program that has to take all the elements of a file (which has several lines) and put in a dictionary like this:
file (1st line) => Mercado1,biscuit,4.3,milk,3.2,juice,7.1,chocolate,6.4,detergent,3.2,beer,6.4,butter,8.7 dictionary => { market 1:{biscuit:4.3, milk:3.2, etc }, market 2:{etc} }
So first I did a function to create the inside dictionaries, those of each market (each row), but when I run it is only storing the last row of the file:
def criaDicProdutos():
    arq = open('MercadosProdutos.txt','r', encoding = 'utf-8')
    dic = {}
    for linha in arq:
        lista_produtos = linha.strip().split(',')
        for i in range (1,len(lista_produtos)-1):
            teste = lista_produtos[i].isalpha()
            if teste == True:
                dic[lista_produtos[i]] = lista_produtos[i+1]
    arq.close()
    print(dic)
    return  di
Below is the txt file I’m using:
Cheap,biscuit,4.3,milk,3.2,juice,7.1,chocolate,6.4,detergent,3.2,beer,6.4,butter,8.7 Ultrak,biscuit,3.5,milk,3.3,juice,8.9,chocolate,6.9,detergent,4.2,beer,6.4,butter,8.7 Market,biscuit,4.5,milk,3.2,juice,7.5,chocolate,6.6,detergent,3.8,beer,6.5,butter,9.2 Preferred,biscuit,4.65,milk,3.4,juice,8.1,chocolate,8.1,detergent,3.3,beer,6.5,butter,8.9 Choice,biscuit,5.2,milk,3.3,juice,8.3,chocolate,7.5,detergent,3.9,beer,6.4,butter,8.6
Great, I got it! Thank you so much
– Tovisck