Home > Net >  how to make list of dictionary with an input from text file
how to make list of dictionary with an input from text file

Time:01-11

So I want to create a list of dictionary with an input from text file like this

Aradea 500 125 100 0 0 
Safwan 300 50 150 0 0
Fatima 250 75 75 0 0

desired output from python

[{'name': 'Aradea', 'price': '500', 'total': 225}
{'name': 'Safwan', 'price': '300', 'total': 200}
{'name': 'Fatima', 'price': '250', 'total': 150}]

so the total is the addition of index[2] [3] [4] [5]

but I got an output. it make a 3 list not 1 list like i'm desired

[{'nama': 'Aradea', 'price': '500', 'total': 225}]
[{'nama': 'Safwan', 'price': '300', 'total': 200}]
[{'nama': 'Fatima', 'price': '250', 'total': 150}]

and my code is like this

def baca_data(filename):
    file = open(filename,"r")
    teks = file.readlines()
    for line in teks:
        line = line.split() # indeks[0] name, indeks[1] price, next index is addiction from index[2] till index[5]
        tabungan = []
        d = {}
      
        isi = [[line[0], line[1], int(line[2])   int(line[3])   int(line[4])   int(line[5])]]
        format = ["name","price","total total"]
        for item in isi:
            for key, value in zip(format,item):
                d[key] = value
        tabungan.append(d)
    
        print(tabungan)

can you guys help me how to fix it#pytho

CodePudding user response:

It's because, the list is created within the loop, and thus it is created newly every iteration. Thus, define the list outside the loop and append the dictionary to it, and print the list after the loop's execution, like this...

def baca_data(filename):
    file = open(filename,"r")
    teks = file.readlines()
    tabungan = []
    for line in teks:
        line = line.split() # indeks[0] name, indeks[1] price, next index is addiction from index[2] till index[5]
        d = {}
      
        isi = [[line[0], line[1], int(line[2])   int(line[3])   int(line[4])   int(line[5])]]
        format = ["name","price","total total"]
        for item in isi:
            for key, value in zip(format,item):
                d[key] = value
        tabungan.append(d)
    
    print(tabungan)

CodePudding user response:

Your problem is that you are creating a new list each iteration. To fix this, you need to move the list declaration outside of the for loop and append to it from there.

def baca_data(filename):
    file = open(filename,"r")
    teks = file.readlines()
    tabungan = [] #move to here
    for line in teks:
        line = line.split() # indeks[0] name, indeks[1] price, next index is addiction from index[2] till index[5]
        # tabungan = [] #problem
        d = {}
      
        isi = [[line[0], line[1], int(line[2])   int(line[3])   int(line[4])   int(line[5])]]
        format = ["name","price","total total"]
        for item in isi:
            for key, value in zip(format,item):
                d[key] = value
        tabungan.append(d)
    
        print(tabungan)

output:

[{'name': 'Aradea', 'price': '500', 'total': 225}
{'name': 'Safwan', 'price': '300', 'total': 200}
{'name': 'Fatima', 'price': '250', 'total': 150}]

CodePudding user response:

You can get the desired result with the following.

def baca_data(filename):

    result = []

    with open(filename, "r") as f:
        for line in f.readlines():
            d = {}
            arr = line.split()
            for i in range(0, len(arr), 2):
                d[arr[i]] = arr[i 1]
            result.append(d)
            
    print(result)

  •  Tags:  
  • Related